COMP9024 ♢ Week 04a ♢ Graph Algorithms ♢ (22T0)

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [0/67]
❖ Directed Graphs

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [1/67]
❖ Directed Graphs (Digraphs)

In our previous discussion of graphs:

In many real-world applications of graphs:
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [2/67]
❖ ... Directed Graphs (Digraphs)

Example digraph and adjacency matrix representation:

[Diagram:Pic/digraph1.png]

Undirectional symmetric matrix
Directional non-symmetric matrix

Maximum #edges in a digraph with V vertices: V2

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [3/67]
❖ ... Directed Graphs (Digraphs)

Terminology for digraphs …

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

Reachability:   w is reachable from v if ∃ directed path v,…,w
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [4/67]
❖ Digraph Applications

Potential application areas:

Domain Vertex Edge
Web web page hyperlink
scheduling task precedence
chess board position legal move
science journal article citation
dynamic data malloc'd object pointer
programs function function call
make file dependency
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [5/67]
❖ ... Digraph Applications

Problems to solve on digraphs:

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [6/67]
❖ Digraph Representation

Similar set of choices as for undirectional graphs:

V vertices identified by 0 .. V-1

[Diagram:Pic/digraph-rep.png]

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [7/67]
❖ Reachability

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [8/67]
❖ Transitive Closure

Given a digraph G  it is potentially useful to know

Example applications:

How to compute transitive closure?

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [9/67]
❖ ... Transitive Closure

One possibility:

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.

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [10/67]
❖ Exercise : Transitive Closure Matrix

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

[Diagram:Pic/digraph1.png]

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [11/67]
Transitive closure of example graph:

[Diagram:Pic/tc.png]

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [12/67]
❖ ... Transitive Closure

Goal: produce a matrix of reachability values

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)

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [13/67]
❖ ... Transitive Closure

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [14/67]
❖ ... Transitive Closure

How it works …

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

After iteration 2, tc[s][t] is 1 if any of the following exist Etc. … so after the Vth iteration, tc[s][t] is 1 if
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [15/67]
❖ Exercise : Transitive Closure

Trace Warshall's algorithm on the following graph:

[Diagram:Pic/tc-graph2.png]

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [16/67]
1st iteration i=0:
tc[0][1][2][3]
[0]0111
[1]1111
[2]0100
[3]0000
2nd iteration i=1:
tc[0][1][2][3]
[0]1111
[1]1111
[2]1111
[3]0000
3rd iteration i=2: unchanged

4th iteration i=3: unchanged

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [17/67]
❖ ... Transitive Closure

Cost analysis:

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

Alternative: use DFS in each call to reachable()
Cost analysis:

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [18/67]
❖ 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
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [19/67]
❖ Example: Web Crawling

Goal: visit every page on the web

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [20/67]
❖ Weighted Graphs

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [21/67]
❖ Weighted Graphs

Graphs so far have considered

Some applications require us to consider Weights can be used in both directed and undirected graphs.
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [22/67]
❖ ... Weighted Graphs

Example: major airline flight routes in Australia

[Diagram:Pic/flights.png]

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [23/67]
❖ ... Weighted Graphs

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

1. Cheapest way to connect all vertices?

2. Cheapest way to get from A to B?
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [24/67]
❖ Exercise : Implementing a Route Finder

If we represent a street map as a graph

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [25/67]
❖ Weighted Graph Representation

Weights can easily be added to:

Both representations work whether edges are directed or not.
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [26/67]
❖ ... Weighted Graph Representation

Adjacency matrix representation with weights:

[Diagram:Pic/adjmatrixw.png]

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [27/67]
❖ ... Weighted Graph Representation

Adjacency lists representation with weights:

[Diagram:Pic/adjlistsw.png]

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [28/67]
❖ ... Weighted Graph Representation

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [29/67]
❖ ... Weighted Graph Representation

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++;
   }
}

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [30/67]
❖ Minimum Spanning Trees

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [31/67]
❖ Exercise : Minimising Wires in Circuits

Electronic circuit designs often need to make the pins of several components electrically equivalent by wiring them together.

[Diagram:Pic/circuit-diagram.png]

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 …

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [32/67]
❖ Minimum Spanning Trees

Reminder: Spanning tree ST of graph G=(V,E)

Minimum spanning tree MST of graph G 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)

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [33/67]
❖ ... Minimum Spanning Trees

Example:

[Diagram:Pic/example-graph.png]

An MST …

[Diagram:Pic/kruskal4.png]

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [34/67]
❖ ... Minimum Spanning Trees

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)

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [35/67]
❖ ... Minimum Spanning Trees

Simplifying assumption:

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [36/67]
❖ Kruskal's Algorithm

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

  1. start with empty MST
  2. consider edges in increasing weight order
    • add edge if it does not form a cycle in MST
  3. repeat until V-1 edges are added

Critical operations:

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [37/67]
❖ ... Kruskal's Algorithm

Execution trace of Kruskal's algorithm:

[Diagram:Pic/kruskal.png]

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [38/67]
❖ Exercise : Kruskal's Algorithm

Show how Kruskal's algorithm produces an MST on:

[Diagram:Pic/example-graph.png]

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [39/67]
After 3rd iteration:

[Diagram:Pic/kruskal1.png]

After 6th iteration:

[Diagram:Pic/kruskal2.png]

After 7th iteration:

[Diagram:Pic/kruskal3.png]

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

[Diagram:Pic/kruskal4.png]

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [40/67]
❖ ... Kruskal's Algorithm

Pseudocode:

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [41/67]
❖ ... Kruskal's Algorithm

Rough time complexity analysis …

Possibilities for cycle checking:
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [42/67]
❖ Prim's Algorithm

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [43/67]
❖ ... Prim's Algorithm

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

[Diagram:Pic/prim.png]

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [44/67]
❖ Exercise : Prim's Algorithm

Show how Prim's algorithm produces an MST on:

[Diagram:Pic/example-graph.png]

Start from vertex 0

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [45/67]
After 1st iteration:

[Diagram:Pic/prim1.png]

After 2nd iteration:

[Diagram:Pic/prim2.png]

After 3rd iteration:

[Diagram:Pic/prim3.png]

After 4th iteration:

[Diagram:Pic/prim4.png]

After 8th iteration (all vertices covered):

[Diagram:Pic/kruskal4.png]

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [46/67]
❖ ... Prim's Algorithm

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [47/67]
❖ ... Prim's Algorithm

Rough time complexity analysis …

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [48/67]
❖ Sidetrack: Priority Queues

Some applications of queues require

Priority Queues (PQueues) provide this via: Time complexity for naive implementation of a PQueue containing N items … Most efficient implementation ("heap") …
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [49/67]
❖ Other MST Algorithms

Boruvka's algorithm … complexity O(E·log V)

Karger, Klein, and Tarjan … complexity O(E)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [50/67]
❖ Shortest Path

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [51/67]
❖ 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

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [52/67]
❖ Single-source Shortest Path (SSSP)

Given: weighted digraph G, source vertex s

Result: shortest paths from s to all other vertices

Example:

[Diagram:Pic/sssp.png]

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [53/67]
❖ 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:

[Diagram:Pic/relaxation.png]

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [54/67]
❖ Dijkstra's Algorithm

One approach to solving single-source shortest path problem …

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

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [55/67]
❖ Exercise : Dijkstra's Algorithm

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

[Diagram:Pic/dijkstra.png]


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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [56/67]

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

dist 01497
pred       0    0     0          

dist 01497 22
pred       0    0     0        3  

dist 01397 12
pred       2    0     0        2  

dist 01397 2012
pred       2    0     0    5    2  

dist 01397 1812
pred       2    0     0    1    2  

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [57/67]
❖ ... Dijkstra's Algorithm

Why Dijkstra's algorithm is correct:

Hypothesis.
(a) For visited sdist[s] is shortest distance from source
(b) For unvisited tdist[t] is shortest distance from source via visited nodes

Proof.
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
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [58/67]
❖ ... Dijkstra's Algorithm

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 svSet ⇒ 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)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [59/67]
❖ All-pair Shortest Path (APSP)

Given: weighted digraph G

Result: shortest paths between all pairs of vertices

Example:

[Diagram:Pic/apsp.png]

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [60/67]
❖ Digraph Applications

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [61/67]
❖ PageRank

Goal: determine which Web pages are "important"

Approach: ignore page contents; focus on hyperlinks

Problem: the Web is a very large graph Assume for the moment that we could build a graph …

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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [62/67]
❖ ... PageRank

Simple PageRank algorithm:

PageRank(myPage):
|  rank=0
|  for each page in the Web do
|  |  if linkExists(page,myPage) then
|  |     rank=rank+1
|  |  end if
|  end for

Note: requires inbound link check

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [63/67]
❖ ... PageRank

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 …

So how to really do it?
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [64/67]
❖ ... PageRank

Approach: the random web surfer

curr=random page, prev=null
for a long time do
|  if curr not in array ranked[] then
|     rank[curr]=0
|  end if
|  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

COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [65/67]
❖ Exercise : Implementing Facebook

Facebook could be considered as a giant "social graph"

What kind of algorithm would …
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [66/67]
❖ Summary


COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [67/67]


Produced: 20 Dec 2021