Discrete Mathematics and

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 – Depth First Search ORD DFW SFO LAX
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Queue C and Data Structures Baojian Hua
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
Breath First Searching & Depth First Searching C and Data Structures Baojian Hua
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
Graph Traversal Discrete Mathematics and Its Applications Baojian Hua
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graph Discrete Mathematics and Its Applications Baojian Hua
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
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Graphs Upon completion you will be able to:
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua
Graphs and Paths : Chapter 15 Saurav Karmakar
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.
© 2010 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
CSC 172 DATA STRUCTURES.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search L0 L1 L2
Breadth-First Search (BFS)
Graphs A New Data Structure
Graphs – Breadth First Search
Graphs Chapter 20.
Depth First Search Neil Tang 4/1/2010
CMSC 341 Graphs.
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
CSC317 Graph algorithms Why bother?
Csc 2720 Instructor: Zhuojun Duan
Breadth-First Search L0 L1 L2
Lecture 12 Graph Algorithms
CS120 Graphs.
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Graphs Graph transversals.
Lecture 10 Algorithm Analysis
Discrete Mathematics and
BFS,DFS Topological Sort
Graphs Chapter 13.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
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),
Depth-First Search D B A C E Depth-First Search Depth-First Search
Richard Anderson Autumn 2016 Lecture 5
Depth-First Search D B A C E Depth-First Search Depth-First Search
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Depth First Search Neil Tang 4/10/2008
Depth-First Search CSE 2011 Winter April 2019.
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search L0 L1 L2 C B A E D F 4/25/2019 3:12 AM
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 16 1 – Graphs Graph Categories Strong Components
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Elementary Graph Algorithms
Richard Anderson Winter 2019 Lecture 6
Breadth-First Search L0 L1 L2 C B A E D F 5/14/ :22 AM
Breadth-First Search L0 L1 L2 C B A E D F 7/28/2019 1:03 PM
Presentation transcript:

Discrete Mathematics and Graph Traversal Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn

BFS and DFS BFS: breath first searching DFS: depth 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 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 a b c d e f

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

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

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

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

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

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

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

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 // color convention: not visited, inQueue, deQueued bfs (g, “a”, strOutput); a b c d e f Queue: a

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 // color convention: not visited, discover, finish dfs (g, “a”, strOutput); print a; a (0, ) b c d e f dfs(a)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 tree edges: a->b, b->e, e->d, c->f forward edges: a->d back edges: d->b, f->f cross edges: c->e a b c d e f

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 12 20 4 2 5 1 From Rosen’s book

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

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

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

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

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

An Example: Hasse Diagram 12 20 4 2 5 1 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 (); 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 12 20 4 2 5 1 Source queue: 1 // color convention: not visited, enQueue, deQueue

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

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

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

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

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

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

DFS-based Algorithm 12 20 4 2 5 1 DFS from 4:

DFS-based Algorithm 12 (1, 2) 20 (3, 4) 4 (0, 5) 2 (7, 8) 5 (9, 10) 1 (6, 11) 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?