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

Slides:



Advertisements
Similar presentations
BackTracking Algorithms
Advertisements

Introduction to Trees Chapter 6 Objectives
Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
B ACKTRACK SEARCH ALGORITHM. B ACKTRACKING Suppose you have to make a series of decisions, among various choices, where You don’t have enough information.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
2/10/03Tucker, Sec Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree.
1 Structures and Strategies for State Space Search 3 3.0Introduction 3.1Graph Theory 3.2Strategies for State Space Search 3.3Using the State Space to Represent.
Backtracking.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Binary Trees Chapter 6.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Dr. Jouhaina Chaouachi Siala
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCE 210 Data Structures and Algorithms
Recursive Objects (Part 4)
Section 8.1 Trees.
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
Binary Trees, Binary Search Trees
CS223 Advanced Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
Trees 7/14/2009.
Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Introduction to Trees Chapter 6 Objectives
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

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

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. 2/20

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 of any 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." B and A are ancestor nodes of F – Path a sequence of nodes in which each node is adjacent to the next one 3/20

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 4/20

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 5/20 Subtree B can be divided into two subtrees, C and D

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 6/20

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) 7/ depth-first traversal breadth-first traversal

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. 8/20

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 9/20

Tree Traversal Preordered depth-first traversal – Root  all subtrees of the node – When the function is returned, “backtracking” start 10/20 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) }

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 11/20

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 12/20

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. 13/20

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 – n 2 possible positions on the chessboard Criterion – “no queens threaten each other”

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

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

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

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); }

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

n-Queens Problem Example – 4-Queens Problem

n-Queens Problem A Backtracking Algorithm for n-Queens Problem – In main() function, we will execute “queens(0)” 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); } 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); }

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 being threatened 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

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; }

[Programming Practice 6] N-Queens Algorithm – Visit 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(int i) public static boolean isPromising(int n) 24/20

[Programming Practice 5] N-Queens Algorithm – The output you should get