Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

Week 5 Graph Algorithms

The document discusses directed graphs and algorithms for graph problems. It covers topics like transitive closure, computing the reachable vertices in a digraph using Warshall's algorithm, and representing digraphs using adjacency matrices and lists.

Uploaded by

MP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Week 5 Graph Algorithms

The document discusses directed graphs and algorithms for graph problems. It covers topics like transitive closure, computing the reachable vertices in a digraph using Warshall's algorithm, and representing digraphs using adjacency matrices and lists.

Uploaded by

MP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Week 5: Graph Algorithms https://www.cse.unsw.edu.au/~cs9024/22T1/lecs/week5/notes.

html

Week 5: Graph Algorithms

Directed Graphs

2/88
Directed Graphs (Digraphs)
In our previous discussion of graphs:

!"an edge indicates a relationship between two vertices


!"an edge indicates nothing more than a relationship

In many real-world applications of graphs:

!"edges are directional (v → w ≠ w → v)

!"edges have a weight (cost to go from v → w)

... Directed Graphs (Digraphs) 3/88

Example digraph and adjacency matrix representation:

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

Undirectional ⇒ symmetric matrix


Directional ⇒ non-symmetric matrix

Maximum #edges in a digraph with V vertices: V2

... Directed Graphs (Digraphs) 4/88

Terminology for digraphs …

Directed path: sequence of n ≥ 2 vertices v1 → v2 → … → vn

!"where (vi,vi+1) ∈ edges(G) for all vi,vi+1 in sequence


!"if v1 = vn, we have a directed cycle

Reachability: w is reachable from v if ∃ directed path v,…,w

5/88
Digraph Applications
Potential application areas:

Domain Vertex Edge

Web web page hyperlink

scheduling task precedence

chess board position legal move

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

science journal article citation

dynamic data malloc'd object pointer

programs function function call

make file dependency

... Digraph Applications 6/88

Problems to solve on digraphs:

!"is there a directed path from s to t? (transitive closure)


!"what is the shortest path from s to t? (shortest path)
!"are all vertices mutually reachable? (strong connectivity)
!"how to organise a set of tasks? (topological sort)
!"which web pages are "important"? (PageRank)
!"how to build a web crawler? (graph traversal)

7/88
Digraph Representation
Similar set of choices as for undirectional graphs:

!"array of edges (directed)


!"vertex-indexed adjacency matrix (non-symmetric)
!"vertex-indexed adjacency lists

V vertices identified by 0 .. V-1

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

!"is vertex t reachable from vertex s?

Example applications:

!"can I complete a schedule from the current state?


!"is a malloc'd object being referenced by any pointer?

How to compute transitive closure?

... Transitive Closure 10/88

One possibility:

!"implement it via hasPath(G,s,t) (itself implemented by DFS or BFS algorithm)


!"feasible if reachable(G,s,t) is infrequent operation

What if we have an algorithm that frequently needs to check reachability?

Would be very convenient/efficient to have:

reachable(G,s,t):
| return G.tc[s][t] // transitive closure matrix

Of course, if V is very large, then this is not feasible.

Exercise #1: Transitive Closure Matrix 11/88

Which reachable s .. t exist in the following graph?

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

Transitive closure of example graph:

... Transitive Closure 13/88

Goal: produce a matrix of reachability values

!"if tc[s][t] is 1, then t is reachable from s


!"if tc[s][t] is 0, then t is not reachable from s

So, how to create this matrix?

Observation:

∀i,s,t ∈ vertices(G):
(s,i) ∈ edges(G) and (i,t) ∈ edges(G) ⇒ tc[s][t] = 1

tc[s][t]=1 if there is a path from s to t of length 2 (s→i→t)

... Transitive Closure 14/88

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

If we implement the above as:

make tc[][] a copy of edges[][]


for all i∈vertices(G) do
for all s∈vertices(G) do
for all t∈vertices(G) do
if tc[s][i]=1 and tc[i][t]=1 then
tc[s][t]=1
end if
end for
end for
end for

then we get an algorithm to convert edges into a tc

This is known as Warshall's algorithm

... Transitive Closure 15/88

How it works …

After iteration 1, tc[s][t] is 1 if

!"either s→t exists or s→0→t exists

After iteration 2, tc[s][t] is 1 if any of the following exist

!"s→t or s→0→t or s→1→t or s→0→1→t or s→1→0→t

Etc. … so after the Vth iteration, tc[s][t] is 1 if

!"there is any directed path in the graph from s to t

Exercise #2: Transitive Closure 16/88

Trace Warshall's algorithm on the following graph:

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

1st iteration i=0:

tc [0] [1] [2] [3]


[0] 0 1 1 1
[1] 1 1 1 1
[2] 0 1 0 0
[3] 0 0 0 0
2nd iteration i=1:
tc [0] [1] [2] [3]
[0] 1 1 1 1
[1] 1 1 1 1
[2] 1 1 1 1
[3] 0 0 0 0
3rd iteration i=2: unchanged

4th iteration i=3: unchanged

... Transitive Closure 18/88

Cost analysis:

!"storage: additional V2 items (each item may be 1 bit)


!"computation of transitive closure: O(V3)

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

!"computation of reachable(): O(1) after having generated tc[][]

Amortisation: would need many calls to reachable() to justify other costs

Alternative: use DFS in each call to reachable()


Cost analysis:

!"storage: cost of queue and set during reachable


!"computation of reachable(): cost of DFS = O(V2) (for adjacency matrix)

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

Solution: breadth-first search with "implicit" graph

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

visit scans page and collects e.g. keywords and links

Weighted Graphs

22/88
Weighted Graphs
Graphs so far have considered

!"edge = an association between two vertices/nodes


!"may be a precedence in the association (directed)

Some applications require us to consider

!"a cost or weight of an association


!"modelled by assigning values to edges (e.g. positive reals)

Weights can be used in both directed and undirected graphs.

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

... Weighted Graphs 23/88

Example: major airline flight routes in Australia

Representation: edge = direct flight; weight = approx flying time (hours)

... Weighted Graphs 24/88

Weights lead to minimisation-type questions, e.g.

1. Cheapest way to connect all vertices?

!"a.k.a. minimum spanning tree problem


!"assumes: edges are weighted and undirected

2. Cheapest way to get from A to B?

!"a.k.a shortest path problem


!"assumes: edge weights positive, directed or undirected

Exercise #3: Implementing a Route Finder 25/88

If we represent a street map as a graph

!"what are the vertices?


!"what are the edges?

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

!"are edges directional?


!"what are the weights?
!"are the weights fixed?

26/88
Weighted Graph Representation
Weights can easily be added to:

!"adjacency matrix representation (0/1 → int or float)


!"adjacency lists representation (add int/float to list node)

Both representations work whether edges are directed or not.

... Weighted Graph Representation 27/88

Adjacency matrix representation with weights:

Note: need distinguished value to indicate "no edge".

... Weighted Graph Representation 28/88

Adjacency lists representation with weights:

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

Note: if undirected, each edge appears twice with same weight

... Weighted Graph Representation 29/88

Sample adjacency matrix implementation in C requires minimal changes to previous Graph ADT:

WGraph.h

// edges are pairs of vertices (end-points) plus positive weight


typedef struct Edge {
Vertex v;
Vertex w;
int weight;
} Edge;

// returns weight, or 0 if vertices not adjacent


int adjacent(Graph, Vertex, Vertex);

... Weighted Graph Representation 30/88

WGraph.c

typedef struct GraphRep {


int **edges; // adjacency matrix storing positive weights
// 0 if nodes not adjacent
int nV; // #vertices
int nE; // #edges
} GraphRep;

void insertEdge(Graph g, Edge e) {


assert(g != NULL && validV(g,e.v) && validV(g,e.w));
if (g->edges[e.v][e.w] == 0) { // edge e not in graph
g->edges[e.v][e.w] = e.weight;
g->nE++;
}

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

int adjacent(Graph g, Vertex v, Vertex w) {


assert(g != NULL && validV(g,v) && validV(g,w));
return g->edges[v][w];
}

Minimum Spanning Trees

Exercise #4: Minimising Wires in Circuits 32/88

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.

What kind of algorithm would …

!"help us find the arrangement with the least amount of wire?

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)

!"spanning = all vertices, tree = no cycles


#"ST is a subgraph of G (G'=(V,E') where E' ⊆ E)
#"ST is connected and acyclic

Minimum spanning tree MST of graph G

!"MST is a spanning tree of G


!"sum of edge weights is no larger than any other ST

Applications: Computer networks, Electrical grids, Transportation networks …

Problem: how to (efficiently) find MST for graph G?

NB: MST may not be unique (e.g. all edges have same weight ⇒ every ST is MST)

... Minimum Spanning Trees 34/88

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

... Minimum Spanning Trees 35/88

Brute force solution:

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

Example of generate-and-test algorithm.

Not useful because #spanning trees is potentially large (e.g. nn-2 for a complete graph with n vertices)

... Minimum Spanning Trees 36/88

Simplifying assumption:

!"edges in G are not directed (MST for digraphs is harder)

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

One approach to computing MST for graph G with V nodes:

1. start with empty MST


2. consider edges in increasing weight order Find least weight in the entire tree
#"add edge if it does not form a cycle in MST
3. repeat until V-1 edges are added

Critical operations:

!"iterating over edges in weight order


!"checking for cycles in a graph

... Kruskal's Algorithm 38/88

Execution trace of Kruskal's algorithm:

Exercise #5: Kruskal's Algorithm 39/88

Show how Kruskal's algorithm produces an MST on:

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

After 3rd iteration:

After 6th iteration:

After 7th iteration:

After 8th iteration (V-1=8 edges added):

... Kruskal's Algorithm 41/88

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

... Kruskal's Algorithm 42/88

Time complexity analysis …

!"sorting edge list is O(E·log E)


!"min V, max E iterations over sorted edges
!"on each iteration …
#"getting next lowest cost edge is O(1)
#"checking whether adding it forms a cycle: cost = ??
$"use DFS … too expensive?
$"could use Union-Find data structure (see Sedgewick Ch.1) to maintain sets of connected components
⇒ loop is O(E·log V)
!"overall complexity O(E·log E) = O(E·log V)

Exercise #6: Kruskal's Algorithm 43/88

Why is O(E·log E) = O(E·log V) in this case?

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

1. at most E = V2 edges ⇒ log E = 2·log V = O(log V)


2. if V > E+1 ⇒ can ignore all unconnected vertices

45/88
Prim's Algorithm MST - Method 2

Another approach to computing MST for graph G=(V,E):

1. start from any vertex v and empty MST


2. choose edge not already in MST to add to MST two vertex, s, t
#"must be incident on a vertex s already connected to v in MST
#"must be incident on a vertex t not already connected to v in MST
#"must have minimal weight of all such edges
3. repeat until MST covers all vertices

Critical operations:

!"checking for vertex being connected in a graph


!"finding min weight edge in a set of edges

... Prim's Algorithm 46/88

Execution trace of Prim's algorithm (starting at s=0):

Exercise #7: Prim's Algorithm 47/88

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

Show how Prim's algorithm produces an MST on:

Start from vertex 0

After 1st iteration:

After 2nd iteration:

After 3rd iteration:

After 4th iteration:

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

After 8th iteration (all vertices covered):

... Prim's Algorithm 49/88

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

Critical operation: finding best edge

... Prim's Algorithm 50/88

Rough time complexity analysis …

!"V iterations of outer loop

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

!"in each iteration …


#"find min edge with set of edges is O(E) ⇒ O(V·E) overall
#"find min edge with priority queue is O(log E) ⇒ O(V·log E) overall

51/88
Sidetrack: Priority Queues
Some applications of queues require

!"items processed in order of "priority"


!"rather than in order of entry (FIFO — first in, first out)

Priority Queues (PQueues) provide this via:

!"join: insert item into PQueue with an associated priority (replacing enqueue)
!"leave: remove item with highest priority (replacing dequeue)

Time complexity for naive implementation of a PQueue containing N items …

!"O(1) for join O(N) for leave

Most efficient implementation ("heap") …

!"O(log N) for join, leave

52/88
Other MST Algorithms
Boruvka's algorithm … complexity O(E·log V)

!"the oldest MST algorithm


!"start with V separate components
!"join components using min cost links
!"continue until only a single component

Karger, Klein, and Tarjan … complexity O(E)

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

!"based on Boruvka, but non-deterministic


!"randomly selects subset of edges to consider
!"for the keen, here's the paper describing the algorithm

Shortest Path

54/88
Shortest Path
Path = sequence of edges in graph G p = (v0,v1), (v1,v2), …, (vm-1,vm)

cost(path) = sum of edge weights along path

Shortest path between vertices s and t

!"a simple path p(s,t) where s = first(p), t = last(p)


!"no other simple path q(s,t) has cost(q) < cost(p)

Assumptions: weighted digraph, no negative weights.

Finding shortest path between two given nodes known as source-target SP problem

Variations: single-source SP, all-pairs SP

Applications: navigation, routing in data networks, …

55/88
Single-source Shortest Path (SSSP)
Given: weighted digraph G, source vertex s

Result: shortest paths from s to all other vertices

!"dist[] V-indexed array of cost of shortest path from s


!"pred[] V-indexed array of predecessor in shortest path from 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)

dist[v] is length of shortest known path from s to v


dist[w] is length of shortest known path from s to w

Relaxation updates data for w if we find a shorter path from s to w:

Relaxation along edge e=(v,w,weight):

!"if dist[v]+weight < dist[w] then


update dist[w]:=dist[v]+weight and pred[w]:=v

57/88
Dijkstra's Algorithm
One approach to solving single-source shortest path problem …

Data: G, s, dist[], pred[] and

!"vSet: set of vertices whose shortest path from s is unknown

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:

dist[] // array of cost of shortest path from s


pred[] // array of predecessor in shortest path from s

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

Exercise #8: Dijkstra's Algorithm 58/88

Show how Dijkstra's algorithm runs on (source node = 0):

[0] [1] [2] [3] [4] [5]


dist 0 ∞ ∞ ∞ ∞ ∞

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 – – – – – –

[0] [1] [2] [3] [4] [5]


dist 0 ∞ ∞ ∞ ∞ ∞
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

... Dijkstra's Algorithm 60/88

Why Dijkstra's algorithm is correct:

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

... Dijkstra's Algorithm 61/88

Time complexity analysis …

Each edge needs to be considered once ⇒ O(E).

Outer loop has O(V) iterations.

Implementing "find s∈vSet with minimum dist[s]"

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

Result: shortest paths between all pairs of vertices

!"dist[][] V×V-indexed matrix of cost of shortest path from vrow to vcol


!"path[][] V×V-indexed matrix of next node in shortest path from vrow to vcol

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…

Data: G, dist[][], path[][] Algorithm:

dist[][] // array of cost of shortest path from s to t


path[][] // array of next node after s on shortest path from s to t

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

Exercise #9: Floyd's Algorithm 64/88

Show how Floyd's algorithm runs on:

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

After 1st iteration i=0: unchanged After 2nd iteration i=1:

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

... Floyd's Algorithm 66/88

Why Floyd's algorithm is correct:

A shortest path from s to t using only nodes from {0,…,i} is the shorter of

!"a shortest path from s to t using only nodes from {0,…,i-1}


!"a shortest path from s to i using only nodes from {0,…,i-1}
plus a shortest path from i to t using only nodes from {0,…,i-1}

Also known as Floyd-Warshall algorithm (can you see why?)

... Floyd's Algorithm 67/88

Cost analysis …

!"initialising dist[][], path[][] ⇒ O(V2)


!"V iterations to update dist[][], path[][] ⇒ O(V3)

Time complexity of Floyd's algorithm: O(V3) (same as Warshall's algorithm for transitive closure)

Network Flow

Exercise #10: Merchandise Distribution 69/88

Lucky Cricket Company …

!"produces cricket balls in Fairfield


!"has a warehouse in Rozelle that stocks them

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:

What kind of algorithm would …

!"help us find the maximum number of crates that can be shipped from Fairfield to Rozelle per day?

70/88
Flow Networks
Flow network …

!"weighted graph G=(V,E)


!"distinct nodes s∈V (source), t∈V (sink)

Edge weights denote capacities Applications:

!"Distribution networks, e.g.


#"source: oil field
#"sink: refinery
#"edges: pipes
!"Traffic flow

... Flow Networks 71/88

Flow in a network G=(V,E) … nonnegative f(v,w) for all vertices v,w∈V such that

!"f(v,w)≤capacity for each edge e=(v,w,capacity) ∈ E


!"f(v,w)=0 if no edge between v and w
!"total flow into a vertex = total flow out of a vertex:

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

Maximum flow … no other flow from s to t has larger value

... Flow Networks 72/88

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

Assume … flow network G=(V,E) and flow f(v,w)

Residual network (V,E'):

!"same vertex set V


!"for each edge v →c w ∈ E …
#"f(v,w) < c ⇒ add edge (v →c-f(v,w) w) to E'
#"f(v,w) > 0 ⇒ add edge (v ←f(v,w) w) to E'

Example:

Forward(Source -> Sink): Unused Capacity


Backward(Sink -> Source): Used Capacity

Exercise #11: Augmenting Paths and Residual Networks 75/88

Find an augmenting path in:

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

and show the residual network after augmenting the flow

1. Augmenting path:

maximum additional flow = 1

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

1. Find a shortest augmenting path


2. Update flow[][] so as to represent residual graph
3. Repeat until no augmenting path can be found

... Edmonds-Karp Algorithm 78/88

Algorithm:

flow[][] // V×V array of current flow

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

visited[] /* array of predecessor nodes on shortest path


from source to sink in residual network */

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

Shortest augmenting path can be found by standard BFS

... Edmonds-Karp Algorithm 79/88

Time complexity analysis …

!"Theorem. The number of augmenting paths needed is at most V·E/2.


⇒ Outer loop has O(V·E) iterations.
!"Finding augmenting path ⇒ O(E) (consider only vertices connected to source and sink ⇒ O(V+E)=O(E))

Overall cost of Edmonds-Karp algorithm: O(V·E2)

Note: Edmonds-Karp algorithm is an implementation of general Ford-Fulkerson method

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

Exercise #12: Edmonds-Karp Algorithm 80/88

Show how Edmonds-Karp algorithm runs on:

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] – – – – – –

augmenting path: 0-1-3-5, df: 2

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 – –

augmenting path: 0-2-4-5, df: 1

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 –

augmenting path: 0-2-3-1-4-5, df: 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"

Approach: ignore page contents; focus on hyperlinks

!"treat Web as graph: page = vertex, hyperlink = directed edge


!"pages with many incoming hyperlinks are important
!"need to compute "incoming degree" for vertices

Problem: the Web is a very large graph

!"approx. 1014 pages, 1015 hyperlinks

Assume for the moment that we could build a graph …

Most frequent operation in algorithm "Does edge (v,w) exist?"

... PageRank 84/88

Simple PageRank algorithm:

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

Note: requires inbound link check

... PageRank 85/88

V = # pages in Web, E = # hyperlinks in Web

Costs for computing PageRank for each representation:

Representation linkExists(v,w) Cost

Adjacency matrix edge[v][w] 1

Adjacency lists inLL(list[v],w) ≅ E/V

Not feasible …

!"adjacency matrix … V ≅ 1014 ⇒ matrix has 1028 cells


!"adjacency list … V lists, each with ≅10 hyperlinks ⇒ 1015 list nodes

So how to really do it?

... PageRank 86/88

Approach: the random web surfer

!"if we randomly follow links in the web …


!"… more likely to re-discover pages with many inbound links

curr=random page, prev=null


for a long time do
| if curr not in array ranked[] then
| rank[curr]=0
| end if

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

Could be accomplished while we crawl web to build search index

Exercise #13: Implementing Facebook 87/88

Facebook could be considered as a giant "social graph"

!"what are the vertices?


!"what are the edges?
!"are edges directional?

What kind of algorithm would …

!"help us find people that you might like to "befriend"?

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

#"Edmonds-Karp (maximum flow)

!"Suggested reading (Sedgewick):


#"digraphs … Ch. 19.1-19.3
#"weighted graphs … Ch. 20-20.1
#"MST … Ch. 20.2-20.4
#"SSP … Ch. 21-21.3
#"network flows … Ch. 22.1-22.2

Produced: 13 Mar 2022

42 of 42 18/3/2022, 1:06 pm

You might also like