CSE 2331/5331
CSE 2331/5331
Topic 9:Basic Graph Alg.
RepresentationsBasic traversal algorithmsTopological sort
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
d e
Un-directed graph ๐ธ๐ธ โค ๐๐
2
Directed graph ๐ธ๐ธ โค ๐๐2
CSE 2331/5331
ba
f
c
d e
Un-directed graphs
CSE 2331/5331
Vertex Degree
deg(v) = degree of v = # edges incident on v
CSE 2331/5331
ba
f
c
d e
Lemma: โ๐ฃ๐ฃ๐๐โ๐๐(๐บ๐บ) deg ๐ฃ๐ฃ๐๐ = 2|๐ธ๐ธ|
Some Special Graphs
Complete graph Path Cycle Planar graph Tree
CSE 2331/5331
CSE 2331/5331
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
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)
CSE 2331/5331
Adjacency Matrix
๐๐ ร ๐๐ matrix ๐ด๐ด ๐ด๐ด ๐๐, ๐๐ = 1 if ๐ฃ๐ฃ๐๐ ,๐ฃ๐ฃ๐๐ is an edge Otherwise, ๐ด๐ด ๐๐, ๐๐ = 0
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.
Sample Graph Algorithm
Input: Graph G represented by adjacency lists
CSE 2331/5331
Running time:O(V + E)
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
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
CSE 2331/5331
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
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-structureto store nodes to be explored.
CSE 2331/5331
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
Pseudo-code
Time complexity:O(V+E)
Use adjacency listrepresentation
CSE 2331/5331
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
BFS tree
A node ๐ฃ๐ฃ is the parent of ๐ข๐ข if ๐ข๐ข was first discovered when exploring ๐ฃ๐ฃ
A BFS tree ๐๐ Root: source node ๐ ๐ Nodes in level ๐๐ of ๐๐ are distance ๐๐ away from ๐ ๐
CSE 2331/5331
Connectivity-Checking
CSE 2331/5331
Time complexity:O(V+E)
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|)
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
Example
Perform DFS starting from ๐ฃ๐ฃ1 What if we add edge ๐ฃ๐ฃ1, ๐ฃ๐ฃ8
CSE 2331/5331
DFS
Time complexity O(V+E)
CSE 2331/5331
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
DFS with DFS Tree
CSE 2331/5331
Example
Perform DFS starting from ๐ฃ๐ฃ1
CSE 2331/5331
Another Connectivity Test
CSE 2331/5331
Traverse Entire Graph
CSE 2331/5331
Remarks
DFS(G, k) 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
Directed Graphs
CSE 2331/5331
Un-directed graph ๐ธ๐ธ โค ๐๐
2
Directed graph Each edge ๐ข๐ข, ๐ฃ๐ฃ is directed from u to v ๐ธ๐ธ โค ๐๐2
CSE 2331/5331
ba
f
c
d e
Vertex Degree
indeg(v) = # edges of the form ๐ข๐ข, ๐ฃ๐ฃ outdeg(v) = # edges of the form ๐ฃ๐ฃ,๐ข๐ข
CSE 2331/5331
Lemma: โ๐ฃ๐ฃ๐๐โ๐๐(๐บ๐บ) indeg ๐ฃ๐ฃ๐๐ = |๐ธ๐ธ| Lemma: โ๐ฃ๐ฃ๐๐โ๐๐(๐บ๐บ) outdeg ๐ฃ๐ฃ๐๐ = |๐ธ๐ธ|
ba
f
c
d e
CSE 2331/5331
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
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)
CSE 2331/5331
Adjacency Matrix
๐๐ ร ๐๐ matrix ๐ด๐ด ๐ด๐ด ๐๐, ๐๐ = 1 if ๐ฃ๐ฃ๐๐ ,๐ฃ๐ฃ๐๐ is an edge Otherwise, ๐ด๐ด ๐๐, ๐๐ = 0
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.
Sample Graph Algorithm
Input: Directed graph G represented by adjacency list
CSE 2331/5331
Running time:O(V + E)
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
Reachability Test
How many (or which) vertices are reachable from a source node, say ๐ฃ๐ฃ1?
CSE 2331/5331
CSE 2331/5331
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
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
Pseudo-code
Time complexity:O(V+E)
Use adjacency listrepresentation
Number of Reachable Nodes
CSE 2331/5331
Time complexity:O(V+E)
Compute # nodesreachable from ๐ฃ๐ฃ๐๐
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
More Example
Is ๐ฃ๐ฃ1 reachable from ๐ฃ๐ฃ12 ? Is ๐ฃ๐ฃ12 reachable from ๐ฃ๐ฃ1 ?
CSE 2331/5331
DFS above can be replaced with BFS
CSE 2331/5331
DFS(G, k);
Topological Sort
CSE 2331/5331
CSE 2331/5331
Directed Acyclic Graph
A directed cycle is a sequence ๐ข๐ข1,๐ข๐ข2, โฆ ,๐ข๐ข๐๐ ,๐ข๐ข1such 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
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
beltshirt
tie
jacket
shoes
sockswatch
Another Example
CSE 2331/5331
Is the sorting order unique?
Why requires DAG?
A topological sorted order of graph G exists if and only if G is a directed acyclic graph (DAG).
CSE 2331/5331
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
Example
CSE 2331/5331
undershorts
pants
beltshirt
tie
jacket
shoes
sockswatch
CSE 2331/5331
Topological Sort โ Simplified Implementation
CSE 2331/5331
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
Remarks
Other topological sort algorithm by using properties of DFS
CSE 2331/5331
Last
Analyzing graph algorithms Adjacency list representation or Adjacency matrix representation
CSE 2331 / 5331
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.
CSE 2331 / 5331
3 5ba
f
c
d e
1 1
42
11 2
Example 1
CSE 2331 / 5331
Assume G is represented by adjacency list
Q is priority-queue implemented by min-heap
Example 2
CSE 2331 / 5331
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?