GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS.

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Advertisements

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs – Depth First Search ORD DFW SFO LAX
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Algorithms and Data Structures
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Breadth-First and Depth-First Search
Chapter 8, Part I Graph Algorithms.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Algorithm : Design & Analysis [14]
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Applications of graph traversals
Graph Traversals Introduction Breadth-First Traversal. The Algorithm.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
1 Data Structures DFS, Topological Sort Dana Shapira.
Graph Traversals CSC 172 SPRING 2002 LECTURE 26. Traversing graphs Depth-First Search like a post-order traversal of a tree Breath-First Search Less like.
Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
CS261 Data Structures DFS and BFS – Edge List Representation.
Search Related Algorithms. Graph Code Adjacency List Representation:
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
DFS Search on Undirected Graphs
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
Topological Sort: Definition
Graphs Upon completion you will be able to:
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Graphs and Paths : Chapter 15 Saurav Karmakar
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Breadth-first and depth-first traversal CS1114
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Principles of graph theory
Graph Algorithms Using Depth First Search
Graph.
Algorithm : Design & Analysis [14]
Depth-First Search Graph Traversals Depth-First Search DFS.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Elementary Graph Algorithms
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS

Graphs - Traversals Task: visit all nodes of a (connected) graph G, starting from a given node. Equivalent formulation of this task: Find a spanning tree T of graph G starting from a given node. Definition Spanning Tree T of G(V,E): T is a tree that contains all vertices of G: T(V T,E T ) V T =V ; E T < E.

4 Figure 1 4 Figure 2

Depth-First Search (DFS) 4 Continue exploration along a path as long as one can continue by adding edges one by one, until on reaches a node already discovered. Then, backtrack and start a new path. Breadth-First Search (BFS) 4 Starting from a node, go in all possible directions to nodes 1 edge away. Then, go one step from each of the newly discovered nodes and repeat until there are no new nodes to discover. Two Main Algorithms

4 Figure 3 (DFS diagram) 4 Figure 4 (BFS diagram)

4 Figure 5 The N-Queens Problem

4 Choose initial vertex (root of DFS tree). Label it O and push incident edges onto stack. Set counter c:=1; 4 Repeat until (stack empty). Take edge xy on top of stack and explore: if y has no label yet: assign it label c; increment c; push all incident edges yz on stack make xy a tree-edge else just pop xy off stack (a back-edge, going to ancestores, ends of tree-edges)

4 Invariant: Top stack vertex x always has a no. already. No back-edge can go to a non ancestor (else would have been explored earlier from another direction, would be a tree edge). O( |E| + |V| ) Because: o Each edge is stacked once at most in each direction o Each vertex requires a constant amount of processing. Analysis

4 Figure 6 Depth_First Search (DFS). Example:

Algorithm 4.2.1: Depth-First Search Input: a connected graph G and a starting vertex v Output: a depth-first spanning tree T and a standard vertex- labeling of G Initialize tree T as vertex v Initialize the set of frontier edges for tree T as empty Set dfnumber(v) = 0 Initialize label counter I := 1 While tree T does not yet span G Update the set of frontier edges for T. Let e be a frontier edge for T whose labeled

endpoints has the largest possible dfnumber. Let w be the unlabeled endpoint of edge e. Add edge e (and vertex w) to tree T. Set dfnumber(w) = i. i = i + 1. Return tree T with its dfnumbers. 4 Notation: For the standard vertex-labeling generated during the depth-first search, the integer-label assigned to a vertex w is denoted df number (w). Algorithm contd

Input: a connected graph G and a starting vertex v Output: a depth-first spanning tree T and a standard vertex- labeling of G Initialize tree T as vertex v Initialize the set of frontier edges for tree T as empty Set dfnumber(v) = 0 Initialize label counter I := 1 While tree T does not yet span G Push onto the stack every frontier edges whose labeled endpoint has df number = i - 1. Pop the top edge from the stack and call it e Algorithm 4.2.2: Depth-First Search (Stack Implementation)

Algorithm contd While edge e is not a frontier edge Pop next edge from the stack and call it e Let w be the unlabeled endpoint of e Add edge e (and vertex w) to tree T Set df number(w) = I I := I + 1 Return tree T with its dfnumbers.

Algorithm 7.3 DFS Skeleton Input: Array adjVertices of adjacency lists that represent a directed graph G = (V,E), as described in Section 7.2.3, and n, the number of vertices. The array is defined for indexes 1….n. Other parameters are as needed bye the application. Output: Return value depends on the application. Return type can vary; int is just an example Remarks: This skeleton is also adequate for some undirected graph problems that ignore nontree edges, but see algorithm 7.8. Color meanings are white = undiscovered, gray = active, black = finished.

Int dfsSweep(IntList[] adjVertices, int n, …) int ans; Allocate color arrya and initialize to white For each vertex v of G, in some order: if (color[v] == white) int vAns = dfs(adjVertices, color, v, …); (Process vAns) //Continue loop return ans;

Int dfs(IntList[] adjVertices, int[] color, int v, …) int w; intList remAdj; int ans; color[v] = gray; Preorder processing of vertix v remAdj = adjVertices[v]; while (remAdj != nil) w = first(remAdj); if (color[w] == white) Exploratory processing for tree edge vw

int wAns = dfs(adjVertices, color, w, …); Backtrack processing for tree edge vw, using wAns else Checking (I.e., processing ) for nontree edge vw remAdj = rest(remAdj) Postorder processing of vertex v, including final computation color[v] = black; return ans

Algorithm 4.2.3: Breadth-First Search Input: a connected graph G and a starting vertex v Output: a breadth-first spanning tree T and a standard vertex- labeling of G Initialize tree T as vertex v Initialize the set of frontier edges for tree T as empty Write label counter i : = 1 Initialize label counter i := 1 While tree T does not yet span G Update the set of frontier edges for T. Let e be a frontier edge for T whose labeled (contd)

endpoints has the largest possible dfnumber. Let w be the unlabeled endpoint of edge e. Add edge e (and vertex w) to tree T. Write label I on vertex w. i = i + 1. Return tree T with its dfnumbers. Algorithm contd

Input: a connected graph G Output: a breadth-first spanning tree T and a standard vertex- labeling of G Initialize tree T as vertex v Initialize the set of frontier edges for tree T as empty Write label 0 on vertex v Initialize label counter i := 1 While tree T does not yet span G Enqueue (to the back of queue) every frontier edge whose labeled endpoint is = i - 1. Dequeue an edge from the front of queue and call it e Algorithm 4.2.4: Breadth-First Search (queue Implementation)

Algorithm contd While edge e is not a frontier edge Dequeue next edge and call it e Let w be the unlabeled endpoint of e Add edge e (and vertex w) to tree T Write label I on vertex w i := i + 1 Return tree T with its dfnumbers. 4 Notation: Whereas the stack (Last-In-First-Out) is the appropriate data structure to store the frontier edges in a DFS, the queue (First-In-First-Out) is most appropriate for the BFS, since the frontier edges that arise earliest are given the highest priority.

Example 7.7 Breadth-first Search 4 Let’s see how breadth-first search works, starting from vertex A in the same graph as we used in Example 7.6. Instead of Terry the tourist, a busload of tourists start walking at A in the left diagram. They spread out and explore in all directions permitted by edges leaving A, looking for bargains. (We still think of edges as one-way bridges, but now they are one-way for walking, as well as traffic.) Figure 8