Download presentation

Presentation is loading. Please wait.

Published byBertha Stevens Modified about 1 year ago

1
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort

2
CSE 2331/5331 What Is A Graph Graph G = (V, E) V: set of nodes E: set of edges Example: V ={ a, b, c, d, e, f } E ={(a, b), (a, d), (a, e), (b, c), (b, d), (b, e), (c, e), (e,f)} ba f c de

3
CSE 2331/5331 ba f c de

4
Un-directed graphs CSE 2331/5331

5
Vertex Degree deg(v) = degree of v = # edges incident on v CSE 2331/5331 ba f c de

6
Some Special Graphs Complete graph Path Cycle Planar graph Tree CSE 2331/5331

7
Representations of Graphs

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

9
CSE 2331/5331 Adjacency Matrix

10
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.

11
Sample Graph Algorithm Input: Graph G represented by adjacency lists CSE 2331/5331 Running time: O(V + E)

12
Connectivity CSE 2331/5331

13
Connectivity Checking How to check if the graph is connected? One approach: Graph traversal BFS: breadth-first search DFS: depth-first search CSE 2331/5331

14
BFS: Breadth-first search

15
CSE 2331/5331 Intuition 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 … Need a data-structure to store nodes to be explored.

16
CSE 2331/5331 Intuition cont.

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

18
CSE 2331/5331 Correctness of Algorithm

19
Connectivity-Checking CSE 2331/5331 Time complexity: O(V+E)

20
CSE 2331/5331 Summary for BFS Starting from source node s, visits remaining nodes of graph from small distance to large distance This is one way to traverse an input graph With some special property where nodes are visited in non-decreasing distance to the source node s. Return distance between s to any reachable node in time O(|V| + |E|)

21
DFS: Depth-First Search Another graph traversal algorithm BFS: Go as broad as possible in the algorithm DFS: Go as deep as possible in the algorithm CSE 2331/5331

22
Example CSE 2331/5331

23
DFS Time complexity O(V+E) CSE 2331/5331

24
Depth First Search Tree CSE 2331/5331

25
DFS with DFS Tree CSE 2331/5331

26
Example CSE 2331/5331

27
More Properties of DFS CSE 2331/5331

28
Example CSE 2331/5331

29
Another Connectivity Test CSE 2331/5331

30
Traverse Entire Graph CSE 2331/5331

31
Remarks CSE 2331/5331

32
Directed Graphs CSE 2331/5331

33
ba f c de

34
Vertex Degree CSE 2331/5331 ba f c de

35
Representations of Graphs

36
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)

37
CSE 2331/5331 Adjacency Matrix

38
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.

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

40
Connectivity CSE 2331/5331

41
Reachability Test CSE 2331/5331

42
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

43
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 …

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

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

46
DFS: Depth-First Search CSE 2331/5331

47
More Example CSE 2331/5331

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

49
Topological Sort CSE 2331/5331

50
Directed Acyclic Graph

51
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

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

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

54
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

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

56
CSE 2331/5331

57
Topological Sort – Simplified Implementation CSE 2331/5331

58
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

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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google