Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 2331/5331 CSE 780: Design and Analysis of Algorithms Lecture 14: Directed Graph BFS DFS Topological sort.

Similar presentations


Presentation on theme: "CSE 2331/5331 CSE 780: Design and Analysis of Algorithms Lecture 14: Directed Graph BFS DFS Topological sort."— Presentation transcript:

1 CSE 2331/5331 CSE 780: Design and Analysis of Algorithms Lecture 14: Directed Graph BFS DFS Topological sort

2 CSE 2331/5331 ba f c de

3 Vertex Degree CSE 2331/5331 ba f c de

4 Representations of Graphs

5 CSE 2331/5331 Adjacency Lists For vertex v V, its adjacency list has size: outdeg(v) decide whether (v, u) E or not in time O(outdeg(v)) Size of data structure (space complexity): O(V+E)

6 CSE 2331/5331 Adjacency Matrix

7 CSE 2331/5331 Adjacency Matrix Size of data structure: O ( V V) Time to determine if (v, u) E : O(1) Though larger, it is simpler compared to adjacency list.

8 Sample Graph Algorithm Input: Directed graph G represented by adjacency list CSE 2331/5331 Running time: O(V + E)

9 Connectivity CSE 2331/5331

10 Reachability Test CSE 2331/5331

11 BFS and DFS The algorithms for BFS and DFS remain the same Each edge is now understood as a directed edge BFS(V,E, s) : visits all nodes reachable from s in non-decreasing order

12 CSE 2331/5331 BFS Starting from source node s, Spread a wavefront to visit other nodes First visit all nodes one edge away from s Then all nodes two edges away from s …

13 CSE 2331/5331 Pseudo-code Time complexity: O(V+E) Use adjacency list representation

14 Number of Reachable Nodes CSE 2331/5331 Time complexity: O(V+E)

15 DFS: Depth-First Search CSE 2331/5331

16 More Example CSE 2331/5331

17 DFS above can be replaced with BFS CSE 2331/5331 DFS(G, k);

18 Topological Sort CSE 2331/5331

19 Directed Acyclic Graph

20 CSE 2331/5331 Topological Sort A topological sort of a DAG G = (V, E) A linear ordering A of all vertices from V If edge (u,v) E => A[u] < A[v] undershorts pants belt shirt tie jacket shoes socks watch

21 Another Example CSE 2331/5331 Is the sorting order unique? Why requires DAG?

22 A topological sorted order of graph G exists if and only if G is a directed acyclic graph (DAG). CSE 2331/5331

23 Question How to topologically sort a given DAG? Intuition: Which node can be the first node in the topological sort order? A node with in-degree 0 ! After we remove this, the process can be repeated. CSE 2331/5331

24 Example CSE 2331/5331 undershorts pants belt shirt tie jacket shoes socks watch

25 CSE 2331/5331

26 Topological Sort – Simplified Implementation CSE 2331/5331

27 Time complexity O(V+E) Correctness: What if the algorithm terminates before we finish visiting all nodes? Procedure TopologicalSort(G) outputs a sorted list of all nodes if and only if the input graph G is a DAG If G is not DAG, the algorithm outputs only a partial list of vertices. CSE 2331/5331

28 Remarks Other topological sort algorithm by using properties of DFS CSE 2331/5331


Download ppt "CSE 2331/5331 CSE 780: Design and Analysis of Algorithms Lecture 14: Directed Graph BFS DFS Topological sort."

Similar presentations


Ads by Google