Graph Traversal Discrete Mathematics and Its Applications Baojian Hua

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Graphs CSE 331 Section 2 James Daly. Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due.
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Minimum Spanning Trees (MSTs) Prim's Algorithm For each vertex not in the tree, keep track of the lowest cost edge that would connect it to the tree This.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs – Depth First Search ORD DFW SFO LAX
© 2004 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
Graph Traversals Introduction Breadth-First Traversal. The Algorithm.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
Queue C and Data Structures Baojian Hua
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
Breadth-First Search1 Part-H3 Breadth-First Search CB A E D L0L0 L1L1 F L2L2.
1 Topological Sort: DFS E C GFB AD A: BDFG B: C: D: E: E F: DE A E H H G: H:
Breath First Searching & Depth First Searching C and Data Structures Baojian Hua
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.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Graph C and Data Structures Baojian Hua
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Graph C and Data Structures Baojian Hua
Queue C and Data Structures Baojian Hua
Tree C and Data Structures Baojian Hua
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graph Discrete Mathematics and Its Applications Baojian Hua
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
1 Topological Sort: DFS F C G A B D E H. 2 F C G A B D E H dfs(A)
Graph Traversal C and Data Structures Baojian Hua
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
IS 2610: Data Structures Graph April 5, 2004.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Chapter 2 Graph Algorithms.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
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.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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.
Depth-First Search Lecture 21: Graph Traversals
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Topological Sort: Definition
Graphs Upon completion you will be able to:
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua
Graphs and Paths : Chapter 15 Saurav Karmakar
CMSC 341 Graphs – DFS Expanded. 2 Depth First Traversal with Finish Times dfs(Graph G) { for (each v  V) d[v] = 0// d = discovery “time” time = 0// “global”
Chapter 05 Introduction to Graph And Search Algorithms.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
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.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
CSC 172 DATA STRUCTURES.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Breadth-First Search (BFS)
CS120 Graphs.
Discrete Mathematics and
Graph Representation (23.1/22.1)
Elementary Graph Algorithms
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Discrete Mathematics and
Elementary Graph Algorithms
Presentation transcript:

Graph Traversal Discrete Mathematics and Its Applications Baojian Hua

BFS and DFS BFS: breath first searching start from one vertex, near to far generates BFS forest flat DFS: depth first searching recursion and back-tracking generates DFS forest narrow

“ graph ” ADT in C: Interface // in file “graph.h” #ifndef GRAPH_H #define GRAPH_H typedef struct graph *graph; typedef void (*tyVisit)(poly); graph newGraph (); void insertVertex (graph g, poly data); void insertEdge (graph g, poly from, poly to); void dfs (graph g, poly start, tyVisit visit); void bfs (graph g, poly start, tyVisit visit); // we’d see more later … #endif

Sample Graph a d b fe c For BFS, associate each vertex with a “ distance ” property. distance(v): the number of edges from the vertex “ start ” to vertex “ v ”, with distance(start)=0

Sample Graph BFS a d b fe c bfs (g, “ a ”, strOutput);

Sample Graph BFS a0a0 d b fe c bfs (g, “ a ”, strOutput); print a;

Sample Graph BFS a0a0 d b1b1 fe c bfs (g, “ a ”, strOutput); print a; // a choice print b;

Sample Graph BFS a0a0 d1d1 b1b1 fe c bfs (g, “ a ”, strOutput); print a; // a choice print b; print d;

Sample Graph BFS a0a0 d1d1 b1b1 f e2e2 c bfs (g, “ a ”, strOutput); print a; // a choice print b; print d; print e;

Sample Graph BFS a0a0 d1d1 b1b1 f e2e2 c0c0 bfs (g, “ a ”, strOutput); print a; // a choice print b; print d; print e; // a choice print c;

Sample Graph BFS a0a0 d1d1 b1b1 f1f1 e2e2 c0c0 bfs (g, “ a ”, strOutput); print a; // a choice print b; print d; print e; // a choice print c; print f;

BFS Algorithm bfs (vertex start, tyVisit visit){ queue q = newQueue (); setDistance (start, 0); //Invariant: all vertices in q have distance property enQueue (q, start); while (q not empty) { vertex current = deQueue (q); int dist = getDistance (current); visit (current); for (each adjacent vertex u of “current”){ if (not visited u){ setDistance (u, dist+1); enQueue (q, u); } }}}

BFS Algorithm void bfsMain (graph g, poly start, tyVisit visit) { vertex startV = searchVertex (g, start); bfs (startV, visit); for (each vertex u in graph g) if (not visited u) bfs (q, u); }

Sample Graph BFS a0a0 d b fe c bfs (g, “ a ”, strOutput); Queue: a // color convention: not visited, inQueue, deQueued

Sample Graph BFS a0a0 d1d1 b1b1 fe c bfs (g, “ a ”, strOutput); print a; Queue: b, d Queue: a // color convention: not visited, inQueue, deQueued

Sample Graph BFS a0a0 d1d1 b1b1 f e2e2 c bfs (g, “ a ”, strOutput); print a; // a choice print b; Queue: b, d Queue: a Queue: d, e // color convention: not visited, inQueue, deQueued

Sample Graph BFS a0a0 d1d1 b1b1 f e2e2 c bfs (g, “ a ”, strOutput); print a; // a choice print b; print d; Queue: e Queue: b, d Queue: a Queue: d, e // color convention: not visited, inQueue, deQueued

Sample Graph BFS a0a0 d1d1 b1b1 f e2e2 c0c0 bfs (g, “ a ”, strOutput); print a; // a choice print b; print d; print e; Queue: Queue: e Queue: b, d Queue: a Queue: d, e Queue: c // color convention: not visited, inQueue, deQueued

Sample Graph BFS a0a0 d1d1 b1b1 f1f1 e2e2 c0c0 bfs (g, “ a ”, strOutput); print a; // a choice print b; print d; print e; // a choice print c; Queue: Queue: e Queue: b, d Queue: a Queue: d, e Queue: c Queue: f // color convention: not visited, inQueue, deQueued

Sample Graph BFS a0a0 d1d1 b1b1 f1f1 e2e2 c0c0 bfs (g, “ a ”, strOutput); print a; // a choice print b; print d; print e; // a choice print c; print f; Queue: Queue: e Queue: b, d Queue: a Queue: d, e Queue: c Queue: f Queue: // color convention: not visited, inQueue, deQueued

Sample Graph DFS a d b fe c Associate a “ discover time ” and a “ finish time ” with each vertex v with: discover (start) = 0

Sample Graph DFS a d b fe c dfs (g, “ a ”, strOutput);

Sample Graph DFS a (0, ) d b fe c dfs (g, “ a ”, strOutput); print a;

Sample Graph DFS a (0, ) d b (1, ) fe c dfs (g, “ a ”, strOutput); print a; // a choice print b;

Sample Graph DFS a (0, ) d b (1, ) f e (2, ) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e;

Sample Graph DFS a (0, ) d (3, ) b (1, ) f e (2, ) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d;

Sample Graph DFS a (0, ) d (3, 4) b (1, ) f e (2, ) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d;

Sample Graph DFS a (0, ) d (3, 4) b (1, ) f e (2, 5) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d;

Sample Graph DFS a (0, ) d (3, 4) b (1, 6) f e (2, 5) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d;

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f e (2, 5) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d;

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f e (2, 5) c (8, ) dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f (9, ) e (2, 5) c (8, ) dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c print f

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f (9, 10) e (2, 5) c (8, ) dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f (9, 10) e (2, 5) c (8, 11) dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c

DFS Algorithm dfs (vertex start, tyVisit visit, time time) { visit (start); setDiscover (start, time++); for (each adjacent vertex u of “start”) if (not visited u) dfs (u, visit, time); setFinish (start, time++); }

DFS Algorithm void dfsMain (graph g, poly start, tyVisit visit) { vertex startV = searchVertex (g, start); time time = newTime (); dfs (startV, visit, time); for (each vertex u in graph g) if (not visited u) dfs (u, visit, time); }

Sample Graph DFS a (0, ) d b fe c dfs (g, “ a ”, strOutput); print a; dfs(a) // color convention: not visited, discover, finish

Sample Graph DFS a (0, ) d b (1, ) fe c dfs (g, “ a ”, strOutput); print a; // a choice print b; dfs(a) => dfs(b) // color convention: not visited, discover, finish

Sample Graph DFS a (0, ) d b (1, ) f e (2, ) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; dfs(a) => dfs(b) => dfs(e) // color convention: not visited, discover, finish

Sample Graph DFS a (0, ) d (3, ) b (1, ) f e (2, ) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; dfs(a) => dfs(b) => dfs(e) => dfs(d) // color convention: not visited, discover, finish

Sample Graph DFS a (0, ) d (3, ) b (1, ) f e (2, ) c dfs (g, “ a ”, natOutput); print a; // a choice print b; print e; print d; dfs(a) => dfs(b) => dfs(e) => dfs(d) => dfs(b)??? // color convention: not visited, discover, finish

Sample Graph DFS a (0, ) d (3, 4) b (1, ) f e (2, ) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; dfs(a) => dfs(b) => dfs(e) // color convention: not visited, discover, finish

Sample Graph DFS a (0, ) d (3, 4) b (1, ) f e (2, 5) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; dfs(a) => dfs(b) // color convention: not visited, discover, finish

Sample Graph DFS a (0, ) d (3, 4) b (1, 6) f e (2, 5) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; dfs(a) // color convention: not visited, discover, finish

Sample Graph DFS a (0, ) d (3, 4) b (1, 6) f e (2, 5) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; dfs(a) =>dfs(d)??? // color convention: not visited, discover, finish

Sample Graph DFS a (0, ) d (3, 4) b (1, 6) f e (2, 5) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; dfs(a) // color convention: not visited, discover, finish

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f e (2, 5) c dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; empty! // color convention: not visited, discover, finish

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f e (2, 5) c (8, ) dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c; dfs(c) // color convention: not visited, discover, finish

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f (9, ) e (2, 5) c (8, ) dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c; print f; dfs(c)=>dfs(f) // color convention: not visited, discover, finish

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f (9, ) e (2, 5) c (8, ) dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c; print f; dfs(c)=>dfs(f)=>dfs(f)??? // color convention: not visited, discover, finish

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f (9, ) e (2, 5) c (8, ) dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c; print f; dfs(c)=>dfs(f)=>dfs(f)??? // color convention: not visited, discover, finish

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f (9, 10) e (2, 5) c (8, ) dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c; print f; dfs(c) // color convention: not visited, discover, finish

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f (9, 10) e (2, 5) c (8, ) dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c; print f; dfs(c)=>dfs(e)??? // color convention: not visited, discover, finish

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f (9, 10) e (2, 5) c (8, 11) dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c; print f; dfs(c) // color convention: not visited, discover, finish

Sample Graph DFS a (0, 7) d (3, 4) b (1, 6) f (9, 10) e (2, 5) c (8, 11) dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c; print f; empty! // color convention: not visited, discover, finish

Edge Classification Once we obtain the DFS (or BFS) spanning trees (forests), the graph edges could be classified according to the trees: tree edges: edges in the trees forward edges: ancestors to descants back edges: descants to ancestors cross edges: others

Edge Classification Example a d b fe c tree edges: a->b, b->e, e->d, c->f forward edges: a->d back edges: d->b, f->f cross edges: c->e

Edge Classification Algorithm Based on discover and finish time, for each edge e=(u, v): if v not visited, e is tree edge if v not finished, e is back edge if v finished if discover(u)<discover(v), e is forward edge if discover(u)>discover(v), e is cross edge

Edge Classification Algorithm dfs (vertex start, tyVisit visit, time time) { visit (start); setDiscover (start, time++); for (each edge e=(start, v)) { if (not visited v) { dfs (v, visit, time); classifyEdge (e, “TreeEdge”); } else { if (not setFinish v) classifyEdge (e, “BackEdge”); else { // leave to you } setFinish (start, time++); }

DFS Algorithm void dfsMain (graph g, poly start, tyVisit visit) { vertex startV = searchVertex (g, start); time time = newTime (); dfs (startV, visit, time); for (each vertex u in graph g) if (not visited u) dfs (u, visit, time); }

BFS and DFS Application #1: Topological Sorting

An Example: Hasse Diagram From Rosen ’ s book

An Example: Hasse Diagram Sorted Sequence: 1

An Example: Hasse Diagram Sorted Sequence: 1, 2

An Example: Hasse Diagram Sorted Sequence: 1, 2, 4

An Example: Hasse Diagram Sorted Sequence: 1, 2, 4, 12

An Example: Hasse Diagram Sorted Sequence: 1, 2, 4, 12, 5

An Example: Hasse Diagram Sorted Sequence: 1, 2, 4, 12, 5, 20

Source-queue Topological Sorting Algorithm topoSortBfs (graph g) { for (each vertex v) calculate in-degree for v; // vertices in queue q are candidates for // deletion queue q = newQueue (); for (each vertex v) if (in-degree of v ==0) enQueue (q, v);

Source-queue Topological Sorting Algorithm (cont ’ ) while (q not empty) { vertex current = deQueue (q); for (each edge e=(current, v)) if (not visited v) { in-degree of v --; if (in-degree of v ==0) enQueue (q, v); } // BFS-based algorithm

An Example: Hasse Diagram Source queue: 1 // color convention: not visited, enQueue, deQueue

An Example: Hasse Diagram Sorted Sequence: 1 Source queue: 2, 5

An Example: Hasse Diagram Sorted Sequence: 1, 2 Source queue: 5, 4

An Example: Hasse Diagram Sorted Sequence: 1, 2, 5 Source queue: 4 // Note that we don ’ t // enQueue 20!!

An Example: Hasse Diagram Sorted Sequence: 1, 2, 5, 4 Source queue: 12, 20 // a chance

An Example: Hasse Diagram Sorted Sequence: 1, 2, 5, 4, 12 Source queue: 20

An Example: Hasse Diagram Sorted Sequence: 1, 2, 5, 4, 12, 20 Source queue: empty!

DFS-based Algorithm DFS from 4:

DFS-based Algorithm 1 (6, 11) 2 (7, 8) 5 (9, 10) 20 (3, 4) 4 (0, 5) 12 (1, 2) DFS from 4: When each vertex v finishes, insert v onto head of a linked list

Topological Sorting Algorithm dfs (vertex start, tyVisit visit, linkedList list) { visit (start); for (each edge e=(start, v)) // as before … linkedListInsertHead (list, start) }

DFS Algorithm linkedList topoSortDfs (graph g, poly start, tyVisit visit) { vertex startV = searchVertex (g, start); linkedList list = newLinkedList (); dfs (startV, visit, list); for (each vertex u in graph g) if (not visited u) dfs (u, visit, list); return list; }

Some Extra Programming Assignments Simple path: is there a simple path from vertex u to v? Or, are vertices u and v connected? Is an undirected g connected? Or, how many connected components are there in g? Cycle detection: is there a cycle in a digraph g? Two colorability: is it possible to color all the vertices in a digraph g using two colors, such that no adjacent vertices are of the same color?