Download presentation
Presentation is loading. Please wait.
1
CSE 2331/5331 Topic 9: Basic Graph Alg.
Representations Basic traversal algorithms Topological sort CSE 2331/5331
2
What Is A Graph Graph G = (V, E) V: set of nodes E: set of edges
b a f c d e 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)} CSE 2331/5331
3
Un-directed graph Directed graph πΈβ€ π 2 πΈβ€ π 2 b a f c d e
πΈβ€ π 2 Directed graph πΈβ€ π 2 b a f c d e CSE 2331/5331
4
Un-directed graphs CSE 2331/5331
5
Vertex Degree deg(v) = degree of v = # edges incident on v
b a f c d e Lemma: π£ π βπ(πΊ) deg π£ π = 2|πΈ| CSE 2331/5331
6
Some Special Graphs Complete graph Path Cycle Planar graph Tree
CSE 2331/5331
7
Representations of Graphs
Adjacency lists Each vertex u has a list, recording its neighbors i.e., all vβs such that (u, v) ο E An array of V lists V[i].degree = size of adj list for node π£ π V[i].AdjList = adjacency list for node π£ π CSE 2331/5331
8
Adjacency Lists For vertex v ο V, its adjacency list
has size: deg(v) decide whether (v, u) ο E or not in time Ξ (deg(v)) Size of data structure (space complexity): Ξ(|V| + |E|) = Ξ (V+E) CSE 2331/5331
9
Adjacency Matrix πΓπ matrix π΄ π΄ π,π =1 if π£ π , π£ π is an edge
Otherwise, π΄ π,π =0 CSE 2331/5331
10
Adjacency Matrix Size of data structure:
Ξ ( V ο΄ V) Time to determine if (v, u) ο E : Ξ(1) Though larger, it is simpler compared to adjacency list. CSE 2331/5331
11
Sample Graph Algorithm
Input: Graph G represented by adjacency lists Running time: Ξ(V + E) CSE 2331/5331
12
Connectivity A path in a graph is a sequence of vertices π’ 1 , π’ 2 ,β¦, π’ π such that there is an edge π’ π , π’ π+1 between any two adjacent vertices in the sequence Two vertices π’, π€βπ πΊ are connected if there is a path in G from u to w. We also say that w is reachable from u. A graph G is connected if every pair of nodes π’, π€βπ πΊ are connected. 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
Input: Given graph G = (V, E), and a source node s ο V Output: Will visit all nodes in V reachable from s For each π£βπ, output a value π£.π π£.π= distance (smallest # of edges) from s to v. π£.π= β if v is not reachable from s. CSE 2331/5331
15
Intuition Starting from source node s, Need a data-structure
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. CSE 2331/5331
16
Intuition cont. A node can be: π£.π :
un-discovered discovered, but not explored explored (finished) π£.π : is set when node v is first discovered. Need a data structure to store discovered but un-explored nodes FIFO ! Queue CSE 2331/5331
17
Pseudo-code Use adjacency list representation Time complexity: Ξ(V+E)
CSE 2331/5331
18
Correctness of Algorithm
A node not reachable from s will not be visited A node reachable from s will be visited π£.π computed is correct: Intuitively, if all nodes k distance away from s are in level k, and no other nodes are in level k, Then all nodes (k+1)-distance away from s must be in level (k+1). Rigorous proof by induction Consider the breadth-first tree generated by algorithm: Any node u from kβth level: d[u] = k Claim: all nodes v with ο€ (s, v) = k are in level k and all nodes from level k has ο€ (s, v) = k CSE 2331/5331
19
BFS tree A node π£ is the parent of π’ if A BFS tree π
π’ was first discovered when exploring π£ A BFS tree π Root: source node π Nodes in level π of π are distance π away from π CSE 2331/5331
20
Connectivity-Checking
Time complexity: Ξ(V+E) CSE 2331/5331
21
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 Ξ (|V| + |E|) CSE 2331/5331
22
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
23
Example Perform DFS starting from π£ 1 What if we add edge π£ 1 , π£ 8
CSE 2331/5331
24
DFS Time complexity Ξ(V+E) CSE 2331/5331
Intuitively, instead of using a FIFO Queue, now we use a LIFO stack. Time complexity Ξ(V+E) CSE 2331/5331
25
Depth First Search Tree
If v is discovered when exploring u Set π£.ππππππ‘=π’ The collection of edges π£.ππππππ‘, π£ form a tree, called Depth-first search tree. CSE 2331/5331
26
DFS with DFS Tree CSE 2331/5331
27
Example Perform DFS starting from π£ 1 CSE 2331/5331
28
Another Connectivity Test
CSE 2331/5331
29
Traverse Entire Graph CSE 2331/5331
30
Remarks DFS(G, k) Same time complexity as BFS
Another way to compute all nodes reachable to the node π£ π Same time complexity as BFS There are nice properties of DFS and DFS tree that we are not reviewing in this class. CSE 2331/5331
31
Directed Graphs CSE 2331/5331
32
Un-directed graph Directed graph πΈβ€ π 2
πΈβ€ π 2 Directed graph Each edge π’,π£ is directed from u to v πΈβ€ π 2 b a f c d e CSE 2331/5331
33
Vertex Degree indeg(v) = # edges of the form π’, π£
outdeg(v) = # edges of the form π£, π’ b a f c d e Lemma: π£ π βπ(πΊ) indeg π£ π = |πΈ| Lemma: π£ π βπ(πΊ) outdeg π£ π = |πΈ| CSE 2331/5331
34
Representations of Graphs
Adjacency lists Each vertex u has a list, recording its neighbors i.e., all vβs such that (u, v) ο E An array of V lists V[i].degree = size of adj list for node π£ π V[i].AdjList = adjacency list for node π£ π CSE 2331/5331
35
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): Ξ(V+E) CSE 2331/5331
36
Adjacency Matrix πΓπ matrix π΄ π΄ π,π =1 if π£ π , π£ π is an edge
Otherwise, π΄ π,π =0 CSE 2331/5331
37
Adjacency Matrix Size of data structure:
Ξ ( V ο΄ V) Time to determine if (v, u) ο E : O(1) Though larger, it is simpler compared to adjacency list. CSE 2331/5331
38
Sample Graph Algorithm
Input: Directed graph G represented by adjacency list Running time: O(V + E) CSE 2331/5331
39
Connectivity A path in a graph is a sequence of vertices π’ 1 , π’ 2 ,β¦, π’ π such that there is an edge π’ π , π’ π+1 between any two adjacent vertices in the sequence Given two vertices π’, π€βπ πΊ , we say that w is reachable from u if there is a path in G from u to w. Note: w is reachable from u DOES NOT necessarily mean that u is reachable from w. CSE 2331/5331
40
Reachability Test How many (or which) vertices are reachable from a source node, say π£ 1 ? CSE 2331/5331
41
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 CSE 2331/5331
42
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 β¦ CSE 2331/5331
43
Pseudo-code Use adjacency list representation Time complexity: Ξ(V+E)
CSE 2331/5331
44
Number of Reachable Nodes
Compute # nodes reachable from π£ π Time complexity: Ξ(V+E) CSE 2331/5331
45
DFS: Depth-First Search
Similarly, DFS remains the same Each edge is now a directed edge If we start with all nodes unvisited, Then DFS(G, π) visits all nodes reachable to node π£ π BFS from previous NumReachable() procedure can be replaced with DFS. CSE 2331/5331
46
More Example Is π£ 1 reachable from π£ 12 ? Is π£ 12 reachable from π£ 1 ?
CSE 2331/5331
47
DFS above can be replaced with BFS
DFS(G, k); DFS above can be replaced with BFS CSE 2331/5331
48
Topological Sort CSE 2331/5331
49
Directed Acyclic Graph
A directed cycle is a sequence π’ 1 , π’ 2 ,β¦, π’ π , π’ 1 such that there is a directed edge between any two consecutive nodes. DAG: directed acyclic graph Is a directed graph with no directed cycles. CSE 2331/5331
50
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 CSE 2331/5331
51
Another Example Why requires DAG? Is the sorting order unique?
CSE 2331/5331
52
A topological sorted order of graph G exists if and only if G is a directed acyclic graph (DAG).
CSE 2331/5331
53
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
54
Example undershorts pants belt shirt tie jacket shoes socks watch
CSE 2331/5331
55
CSE 2331/5331
56
Topological Sort β Simplified Implementation
CSE 2331/5331
57
Time complexity Correctness: Ξ(V+E)
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
58
Remarks Other topological sort algorithm by using properties of DFS
CSE 2331/5331
59
Last Analyzing graph algorithms Adjacency list representation or
Adjacency matrix representation CSE 2331 / 5331
60
Edge-weighted un-directed graph G = (V, E) and edge weight function π€:πΈβπ
E.g, road network, where each node is a city, and each edge is a road, and weight is the length of this road. 3 5 b a f c d e 1 1 2 1 1 2 4 CSE 2331 / 5331
61
Example 1 Assume G is represented by adjacency list
Q is priority-queue implemented by min-heap CSE 2331 / 5331
62
Example 2 Assume G is represented by adjacency matrix
What if move line 10 to above line 8? What if move line 10 to above line 9? CSE 2331 / 5331
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.