Week 5 Graph Algorithms
Week 5 Graph Algorithms
html
Directed Graphs
2/88
Directed Graphs (Digraphs)
In our previous discussion of graphs:
1 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
5/88
Digraph Applications
Potential application areas:
2 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
7/88
Digraph Representation
Similar set of choices as for undirectional graphs:
3 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Reachability
9/88
Transitive Closure
Given a digraph G it is potentially useful to know
Example applications:
One possibility:
reachable(G,s,t):
| return G.tc[s][t] // transitive closure matrix
4 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Observation:
∀i,s,t ∈ vertices(G):
(s,i) ∈ edges(G) and (i,t) ∈ edges(G) ⇒ tc[s][t] = 1
5 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
How it works …
6 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Cost analysis:
7 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
19/88
Digraph Traversal
Same algorithms as for undirected graphs:
depthFirst(v):
1. mark v as visited
2. for each (v,w)∈edges(G) do
if w has not been visited then
depthFirst(w)
breadth-first(v):
1. enqueue v
2. while queue not empty do
dequeue v
if v not already visited then
mark v as visited
enqueue each vertex w adjacent to v
20/88
Example: Web Crawling
Goal: visit every page on the web
8 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
webCrawl(startingURL):
| mark startingURL as alreadySeen
| enqueue(Q,startingURL)
| while Q is not empty do
| | nextPage=dequeue(Q)
| | visit nextPage
| | for each hyperLink on nextPage do
| | | if hyperLink not alreadySeen then
| | | mark hyperLink as alreadySeen
| | | enqueue(Q,hyperLink)
| | | end if
| | end for
| end while
Weighted Graphs
22/88
Weighted Graphs
Graphs so far have considered
9 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
10 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
26/88
Weighted Graph Representation
Weights can easily be added to:
11 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Sample adjacency matrix implementation in C requires minimal changes to previous Graph ADT:
WGraph.h
WGraph.c
12 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Electronic circuit designs often need to make the pins of several components electrically equivalent by wiring them together.
To interconnect a set of n pins we can use an arrangement of n-1 wires each connecting two pins.
13 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
33/88
Minimum Spanning Trees
Reminder: Spanning tree ST of graph G=(V,E)
NB: MST may not be unique (e.g. all edges have same weight ⇒ every ST is MST)
Example:
An MST …
14 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
findMST(G):
| Input graph G
| Output a minimum spanning tree of G
|
| bestCost=∞
| for all spanning trees t of G do
| | if cost(t)<bestCost then
| | bestTree=t
| | bestCost=cost(t)
| | end if
| end for
| return bestTree
Not useful because #spanning trees is potentially large (e.g. nn-2 for a complete graph with n vertices)
Simplifying assumption:
37/88
Kruskal's Algorithm MST - Method 1
15 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Critical operations:
16 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Pseudocode:
17 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
KruskalMST(G):
| Input graph G with n nodes
| Output a minimum spanning tree of G
|
| MST=empty graph
| sort edges(G) by weight
| for each e∈sortedEdgeList do
| | MST = MST ∪ {e}
| | if MST has a cyle then
| | MST = MST \ {e}
| | end if
| | if MST has n-1 edges then
| | return MST
| | end if
| end for
18 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
45/88
Prim's Algorithm MST - Method 2
Critical operations:
19 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
20 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Pseudocode:
PrimMST(G):
| Input graph G with n nodes
| Output a minimum spanning tree of G
|
| MST=empty graph
| usedV={0}
| unusedE=edges(g)
| while |usedV|<n do
| | find e=(s,t,w)∈unusedE such that {
| | s∈usedV, t∉usedV and w is min weight of all such edges
| | }
| | MST = MST ∪ {e}
| | usedV = usedV ∪ {t}
| | unusedE = unusedE \ {e}
| end while
| return MST
21 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
51/88
Sidetrack: Priority Queues
Some applications of queues require
!"join: insert item into PQueue with an associated priority (replacing enqueue)
!"leave: remove item with highest priority (replacing dequeue)
52/88
Other MST Algorithms
Boruvka's algorithm … complexity O(E·log V)
22 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Shortest Path
54/88
Shortest Path
Path = sequence of edges in graph G p = (v0,v1), (v1,v2), …, (vm-1,vm)
Finding shortest path between two given nodes known as source-target SP problem
55/88
Single-source Shortest Path (SSSP)
Given: weighted digraph G, source vertex s
23 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Example:
56/88
Edge Relaxation
Assume: dist[] and pred[] as above (but containing data for shortest paths discovered so far)
57/88
Dijkstra's Algorithm
One approach to solving single-source shortest path problem …
24 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Algorithm:
dijkstraSSSP(G,source):
| Input graph G, source node
|
| initialise dist[] to all ∞, except dist[source]=0
| initialise pred[] to all -1
| vSet=all vertices of G
| while vSet≠∅ do
| | find s∈vSet with minimum dist[s]
| | for each (s,t,w)∈edges(G) do
| | relax along (s,t,w)
| | end for
| | vSet=vSet\{s}
| end while
25 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
pred – – – – – –
dist 0 14 9 7 ∞ ∞
pred – 0 0 0 – –
dist 0 14 9 7 ∞ 22
pred – 0 0 0 – 3
dist 0 13 9 7 ∞ 12
pred – 2 0 0 – 2
dist 0 13 9 7 20 12
pred – 2 0 0 5 2
dist 0 13 9 7 18 12
pred – 2 0 0 1 2
Hypothesis.
(a) For visited s … dist[s] is shortest distance from source
(b) For unvisited t … dist[t] is shortest distance from source via visited nodes
Proof.
26 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Base case: no visited nodes, dist[source]=0, dist[s]=∞ for all other nodes
Induction step:
1. If s is unvisited node with minimum dist[s], then dist[s] is shortest distance from source to s:
#"if ∃ shorter path via only visited nodes, then dist[s] would have been updated when processing the predecessor of s on this path
#"if ∃ shorter path via an unvisited node u, then dist[u]<dist[s], which is impossible if s has min distance of all unvisited nodes
2. This implies that (a) holds for s after processing s
3. (b) still holds for all unvisited nodes t after processing s:
#"if ∃ shorter path via s we would have just updated dist[t]
#"if ∃ shorter path without s we would have found it previously
1. try all s∈vSet ⇒ cost = O(V) ⇒ overall cost = O(E + V2) = O(V2)
2. using a PQueue to implement extracting minimum
#"can improve overall cost to O(E + V·log V) (for best-known implementation)
62/88
All-pair Shortest Path (APSP)
Given: weighted digraph G
27 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
Example:
63/88
Floyd's Algorithm
One approach to solving all-pair shortest path problem…
floydAPSP(G):
| Input graph G
|
| initialise dist[s][t]=0 for each s=t
| =w for each (s,t,w)∈edges(G)
| =∞ otherwise
| initialise path[s][t]=t for each (s,t,w)∈edges(G)
| =-1 otherwise
| for all i∈vertices(G) do
| | for all s∈vertices(G) do
| | | for all t∈vertices(G) do
| | | | if dist[s][i]+dist[i][t] < dist[s][t] then
| | | | dist[s][t]=dist[s][i]+dist[i][t]
28 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
| | | | path[s][t]=path[s][i]
| | | | end if
| | | end for
| | end for
| end for
dist [0] [1] [2] [3] [4] [5] path [0] [1] [2] [3] [4] [5]
[0] 0 14 9 7 [0] 1 2 3
[1] 0 5 [1] 4
[2] 4 0 3 [2] 1 5
[3] 10 0 15 [3] 2 5
[4] 0 [4]
[5] 2 0 [5] 4
dist [0] [1] [2] [3] [4] [5] path [0] [1] [2] [3] [4] [5]
29 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
[0] 0 14 9 7 19 ∞ [0] – 1 2 3 1 –
[1] ∞ 0 ∞ ∞ 5 ∞ [1] – – – – 4 –
[2] ∞ 4 0 ∞ 9 3 [2] – 1 – – 1 5
[3] ∞ ∞ 10 0 ∞ 15 [3] – – 2 – – 5
[4] ∞ ∞ ∞ ∞ 0 ∞ [4] – – – – – –
[5] ∞ ∞ ∞ ∞ 2 0 [5] – – – – 4 –
After 3rd iteration i=2:
dist [0] [1] [2] [3] [4] [5] path [0] [1] [2] [3] [4] [5]
[0] 0 13 9 7 18 12 [0] – 2 2 3 2 2
[1] ∞ 0 ∞ ∞ 5 ∞ [1] – – – – 4 –
[2] ∞ 4 0 ∞ 9 3 [2] – 1 – – 1 5
[3] ∞ 14 10 0 19 13 [3] – 2 2 – 2 2
[4] ∞ ∞ ∞ ∞ 0 ∞ [4] – – – – – –
[5] ∞ ∞ ∞ ∞ 2 0 [5] – – – – 4 –
After 4th iteration i=3: unchanged After 5th iteration i=4: unchanged After 6th iteration i=5:
dist [0] [1] [2] [3] [4] [5] path [0] [1] [2] [3] [4] [5]
[0] 0 13 9 7 14 12 [0] – 2 2 3 2 2
[1] ∞ 0 ∞ ∞ 5 ∞ [1] – – – – 4 –
[2] ∞ 4 0 ∞ 5 3 [2] – 1 – – 5 5
[3] ∞ 14 10 0 15 13 [3] – 2 2 – 2 2
[4] ∞ ∞ ∞ ∞ 0 ∞ [4] – – – – – –
[5] ∞ ∞ ∞ ∞ 2 0 [5] – – – – 4 –
30 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
A shortest path from s to t using only nodes from {0,…,i} is the shorter of
Cost analysis …
Time complexity of Floyd's algorithm: O(V3) (same as Warshall's algorithm for transitive closure)
Network Flow
31 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
!"ships them from factory to warehouse by leasing space on trucks with limited capacity:
!"help us find the maximum number of crates that can be shipped from Fairfield to Rozelle per day?
70/88
Flow Networks
Flow network …
Flow in a network G=(V,E) … nonnegative f(v,w) for all vertices v,w∈V such that
32 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
∑ ∑
f (x, v) = f (v, y) for all v ∈ V \ {s,t}
x∈V y∈V
Example:
A (maximum) flow …
73/88
Augmenting Paths
Assume … f(v,w) contains current flow
Augmenting path: any path from source s to sink t that can currently take more flow
Example:
33 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
74/88
Residual Network Usage: Where is the capacity
Example:
34 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
1. Augmenting path:
2. Residual network:
Can you find a further augmenting path in the new residual network?
77/88
Edmonds-Karp Algorithm
One approach to solving maximum flow problem …
maxflow(G):
Algorithm:
35 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
maxflow(G):
| Input flow network G with source s and sink t
| Output maximum flow value
|
| initialise flow[v][w]=0 for all vertices v, w
| maxflow=0
| while ∃shortest augmenting path visited[] from s to t do
| | df = maximum additional flow via visited[]
| | // adjust flow so as to represent residual graph
| | v=t
| | while v≠s do
| | | flow[visited[v]][v] = flow[visited[v]][v] + df;
| | | flow[v][visited[v]] = flow[v][visited[v]] - df;
| | | v=visited[v]
| | end while
| | maxflow=maxflow+df
| end while
| return maxflow
36 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
flow [0] [1] [2] [3] [4] [5] c-f [0] [1] [2] [3] [4] [5]
[0] [0] 2 3
[1] [1] 3 1
[2] [2] 1 1
[3] [3] 2
[4] [4] 3
[5] [5]
flow [0] [1] [2] [3] [4] [5] c-f [0] [1] [2] [3] [4] [5]
[0] 0 0 0 0 0 0 [0] – 2 3 – – – From BFS
[1] 0 0 0 0 0 0 [1] – – – 3 1 – Node 0 1 2 3 4 5
Pred 0 0 0 1 1 3
[2] 0 0 0 0 0 0 [2] – – – 1 1 –
[3] 0 0 0 0 0 0 [3] – – – – – 2 5-3-1-0
[4] 0 0 0 0 0 0 [4] – – – – – 3
[5] 0 0 0 0 0 0 [5] – – – – – –
37 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
flow [0] [1] [2] [3] [4] [5] c-f [0] [1] [2] [3] [4] [5]
[0] 0 2 0 0 0 0 [0] – 0 3 – – –
[1] -2 0 0 2 0 0 [1] 2 – – 1 1 –
[2] 0 0 0 0 0 0 [2] – – – 1 1 –
[3] 0 -2 0 0 0 2 [3] – 2 – – – 0
[4] 0 0 0 0 0 0 [4] – – – – – 3
[5] 0 0 0 -2 0 0 [5] – – – 2 – –
flow [0] [1] [2] [3] [4] [5] c-f [0] [1] [2] [3] [4] [5]
[0] 0 2 1 0 0 0 [0] – 0 2 – – –
[1] -2 0 0 2 0 0 [1] 2 – – 1 1 –
[2] -1 0 0 0 1 0 [2] 1 – – 1 0 –
[3] 0 -2 0 0 0 2 [3] – 2 – – – 0
[4] 0 0 -1 0 0 1 [4] – – 1 – – 2
[5] 0 0 0 -2 -1 0 [5] – – – 2 1 –
flow [0] [1] [2] [3] [4] [5] c-f [0] [1] [2] [3] [4] [5]
[0] 0 2 2 0 0 0 [0] – 0 1 – – –
[1] -2 0 0 1 1 0 [1] 2 – – 2 0 –
[2] -2 0 0 1 1 0 [2] 2 – – 0 0 –
[3] 0 -1 -1 0 0 2 [3] – 1 1 – – 0
38 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
[4] 0 -1 -1 0 0 2 [4] – 1 1 – – 1
[5] 0 0 0 -2 -2 0 [5] – – – 2 2 –
Digraph Applications
83/88
PageRank
Goal: determine which Web pages are "important"
PageRank(myPage):
| rank=0
| for each page in the Web do
| | if linkExists(page,myPage) then
| | rank=rank+1
| | end if
39 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
| end for
Not feasible …
40 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
| rank[curr]=rank[curr]+1
| if random(0,100)<85 then // with 85% chance ...
| prev=curr
| curr=choose hyperlink from curr // ... crawl on
| else
| curr=random page // avoid getting stuck
| prev=null
| end if
end for
88/88
Summary
!"Digraphs, weighted graphs: representations, applications
!"Reachability
#"Warshall
!"Minimum Spanning Tree (MST)
#"Kruskal, Prim
!"Shortest path problems
#"Dijkstra (single source SPP)
#"Floyd (all-pair SSP)
!"Flow networks
41 of 42 18/3/2022, 1:06 pm
Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.html
42 of 42 18/3/2022, 1:06 pm