1 / 25

Algorithms

The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang. Algorithms. April-May 2013. Dr. Youn-Hee Han. Tree. Tree consists of A finite set of nodes (vertices) A finite set of branches (edges, arcs) that connects the nodes

Download Presentation

Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Project for the Establishing the Korea ㅡ Vietnam College of Technology in BacGiang Algorithms April-May 2013 Dr. Youn-Hee Han

  2. Tree • Tree consists of • A finite set of nodes (vertices) • A finite set of branches (edges, arcs) that connects the nodes • Degree of a node: # of branches • In-degree: # of branch toward the node (# of upward branch) • Out-degree: # of branch away from the node (# of downward branch) • Every non-empty tree has a root node whose in-degree is zero. • In-degree of all other nodes except the root node is one.

  3. Tree • Terminology • A node is a structure which may contain a value or condition • A node is child of its predecessor • E and F are child nodes of B • A node is parent of its successor nodes • B is the parent node of E and F • A is the parent node of B, C, and D • A descendant node ofany node A is any node below A in a tree, where "below" means "away from the root." • B, E, F, C, and D are descendant nodes of A • An ancestor node of any node A is any node above A in a tree, where "above" means "toward the root." • Band A are ancestor nodes of F • Path • a sequence of nodes in which each nodeis adjacent to the next one

  4. Tree • Terminology • A sibling node of A is a node with the same parent as A • B, C, and D are sibling nodes each other • E and F are sibling nodes each other • A leaf (or terminal) node is a node who hos no child nodes • C, D, E, E are leaf nodes • Node with out-degree zero

  5. Tree • Terminology • A subtree of a tree T is a tree consisting of a node in T and all of its descendants in T • each node corresponds to the subtree of itself and all its descendants • each node is the root node of the subtree it determines • the subtree corresponding to the root node is the entire tree Subtree B can be divided into two subtrees, C and D

  6. Tree • Recursive Definitions of Tree • A tree is a set of nodes that either: • 1. is Empty, or • 2. has a designated node, called the root, from which hierarchically descend zero or more subtrees, which are also trees. • Binary tree is a tree in which no node can have more than two subtrees • the child nodes are called left and right • Left/right subtrees are also binary trees

  7. 1 1 5 3 2 2 3 4 5 4 6 6 7 7 Tree Traversal • Tree Traversal • process each node once and only once in a predetermined sequence • Two general approach • Depth-first traversal (depth-first search: DFS) • Breadth-first traversal (breadth-first search: BFS, level-order) breadth-first traversal depth-first traversal

  8. Tree Traversal • Depth-first traversal • proceeds along a path from the root to the most distant descendent of that first child before processing a second child.

  9. Tree Traversal • 3 types of depth-first traversal in Binary Tree • Preorder traversal • A node  left subtree right subtree • Inorder traversal • Left subtree a node right subtree • Postorder traversal • Left subtreeright subtree a node

  10. Tree Traversal • Preordered depth-first traversal • Root  all subtrees of the node • When the function is returned, “backtracking” start void depth_first_tree_search(node v){ node u; visit v; // or process v for (each child u of v) // from left to right depth_first_tree_search(u) }

  11. Tree Traversal • Breadth-first traversal (=level-order traversal) • Begins at the root node and explores all the neighboring nodes • Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal  Attempts to visit the node, not already visited, closest to the root

  12. Backtracking • Maze • When you reached a dead end, you’d go back to a fork and pursue another path. • Think how much easier it would be, if there were a sign, positioned to the dead end, to told you that the path led to nothing but dead ends

  13. Backtracking • “Backtracking” is used to… • Solve problems in which a sequence of objects is chosen from a specified set so that the sequence satisfies some criterion • Such problem is called constraint satisfaction problem • It usually utilizes • tree data structure • tree traversal • In particular, Depth First Search (DFS) • If a more traversal (search) to current direction is determined to be useless, just backtrack to the choice point (sign). • If there is no choice points, the search to solve the problem comes to fail.

  14. n-Queens Problem • n-Queens Problem • The classic example of the use of backtraking • Goal • Position n queens on an n * n chessboard so that no two queens threaten each other • “no queens threaten each other” means that no two queens may be in the same row, column, or diagonal. • In backtracking algorithm… • A sequence of objects • The n positions where the n queens are placed • A specified set • n2 possible positions on the chessboard • Criterion • “no queens threaten each other”

  15. n-Queens Problem • 4-Queens Problem • # of places in the first row= 4 • # of places in the second row = 4 • # of places in the third row = 4 • # of places in the fourth row = 4 4  4  4  4 = 256 # of candidate solutions

  16. n-Queens Problem • Tree for candidate solutions (i,j)  queens’ position (row: i, column:j) The first queen’s possible position The second queen’s possible position The third queen’s possible position The fourth queen’s possible position 후보답: Candidate Solution

  17. n-Queens Problem • Basic backtracking strategy • A candidate solution is a path from the root to a leaf node • Use “depth-first traversal” • Should we traverse all possible paths? • 256 paths in 4-Queens Problem • No! we can traverse only promising node. • We meet a non-promising node, we go back to the node’s parent and proceed with the search on the next child • Promising node • We call a node non-promising if when visiting the node we determine that it cannot possibly lead to a solution • Otherwise, we call it promising

  18. n-Queens Problem • Backtracking strategy • Do “depth-first traversal” of candidate solution tree • Checking whether each node is promising • If it is non-promising, we do prunning the candidate solution tree, and backtracking to the node’s parent • Rough sketch of backtracking void checknode(node v){ node u; if (promising(v))// it depends on the problem to be solved if (there is a solution at v) write the solution; else for (each child u of v) checknode(u); }

  19. n-Queens Problem • Example – 4-Queens Problem • # of possible traversed nodes= 256 • # of traversed nodes in backtracking algorithm = 27

  20. n-Queens Problem • Example – 4-Queens Problem

  21. n-Queens Problem • A Backtracking Algorithm for n-Queens Problem • In main() function, we will execute “queens(0)” void checknode(node v){ node u; if (promising(v)) if (there is a solution at v) write the solution; else for (each child u of v) checknode(u); } void queens(index i){ index j; if(promising(i)) if(i == n) System.out.print(col[1] through col[n]); else for(j=1 ; j < n ; j++) { col[i+1] = j; // select the next location queens(i+1); } }

  22. n-Queens Problem • Promising function in n-Queens Problem • It should check whether two queens are in the same column • col[i] = col[k] • It should also check whether two queens are in the same diagonal • |col[i] – col[k]| = |i – k| • The queen in row 6 is beingthreatened in its left diagonal by the queen in row 3, and its right diagonal by the queen in row 2 • col(6) – col(2) = 4 – 8 = -4 = 2 – 6 • Col(6) – col(3) = 4 – 1 = 3 = 6 – 3

  23. n-Queens Problem • Promising function in n-Queens Problem boolean promising(index i){ index k = 1; boolean switch = true; while(k < i && switch) { // k <= n if(col[i] == col[k] || abs(col[i]-col[k]) == i-k) switch = false; k++; } return switch; }

  24. [Programming Practice 6] • N-QueensAlgorithm • Visit • http://link.koreatech.ac.kr/courses/2013_1/AP-KOICA/AP-KOICA20131.html • Download “Nqueens.java” and run it • Analyze the source codes • Complete the source codes while insert right codes within the two functions • public static void queens(inti) • public static booleanisPromising(int n)

  25. [Programming Practice 5] • N-Queens Algorithm • The output you should get

More Related