Week 08


Reachability


Transitive Closure2/34

Given a digraph g  it is potentially useful to know

Could be encapsulated as

bool reachable(Graph g, Vertex s, Vertex t)

Example applications:

How to compute transitive closure?


... Transitive Closure3/34

How to compute transitive closure?

One possibility:

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

Would be very convenient/efficient to have a look-up table:

int reachable(Graph g, Vertex s, Vertex t)
{
    return (g->tc[s][t]);
}

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


... Transitive Closure4/34

Goal: produce a matrix of reachability values

Example: which reachable s..t exist in the following graph?

[Diagram:Pics/graphs/tc0-small.png]


... Transitive Closure5/34

Transitive Closure matrix for more complex graph:

[Diagram:Pics/graphs/tc-small.png]


Reachability matrix = transitive closure (tc) under connection


... Transitive Closure6/34

A possible implementation to compute Transitive Closure Matrix:

for each edge in G
   copy edge[v][w] to tc[v][w]

for (i = 0; i < V; i++) {
   for (s = 0; s < V; s++) {

      for (t = 0; t < V; t++) {
         if (tc[s][i] && tc[i][t]) 
             tc[s][t] = 1;
      }

   }
}

then we get an algorithm to convert e into a tc

This is know as Warshall's algorithm


Exercise 1: Transitive Closure Matrix7/34

Trace Warshall's algorithm on the following graph:

 

[Diagram:Pics/graphs/tc-graph2-small.png]


... Transitive Closure8/34

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

4th iteration i=3: unchanged


Exercise 2: Transitive Closure9/34

Trace Warshall's algorithm on the following graph:

[Diagram:Pics/graphs/tc-example-small.png]


... Transitive Closure10/34

Cost analysis (tc[][]):

Amortization: many calls to reachable() would justify other costs

Alternative: use DFS in each call to reachable()

Cost analysis (DFS):


Bitmap Transitive Closure (TC) Matrix11/34

The space cost of a Transitive Closure (TC) matrix implemented as

int **tc;
// or even 
unsigned char **tc

would be significant.

We could improve the actual cost by implementing as

BitMap tc;

which uses just one bit for each entry in the TC.

Note: does not improve asymptotic behaviour


Bitmap Transitive Closure (TC) Matrix (cont)12/34

Implement the BitMap ADT (square boolean matrix):

typedef struct {
   unsigned int dimension;
   unsigned int *bits;
} *BitMap;
BitMap newBitMap(int dimension);
void   setBit(BitMap b, int i, int j);
int    unsetBit(BitMap b, int i, int j);
int    isSet(BitMap b, int i, int j);

What a BitMap looks like

[Diagram:Pics/misc/bitmap-small.png]


Weighted Graphs


Weighted Graphs14/34

Graphs so far have considered

Some applications require us to consider Weights can be used in both directed and non-directed graphs.


... Weighted Graphs15/34

Example: major airline flight routes in Australia

[Diagram:Pics/wgraphs/flights-small.png]

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


... Weighted Graphs16/34

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

1. Cheapest way to connect all vertices?

2. Cheapest way to get from A to B?


Exercise 3: Implementing a Route Finder17/34

If we represent a street map as a graph

What kind of algorithm would ...


Exercise 4: Implementing Facebook18/34

Facebook could be considered as a giant "social graph"

What kind of algorithm would ...


Weighted Graph Representations19/34

Adjacency matrix representation with weights:

[Diagram:Pics/wgraphs/adjmatrixw-small.png]

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


... Weighted Graph Representations20/34

Adjacency lists representation with weights:

[Diagram:Pics/wgraphs/adjlistsw-small.png]

Note: if non-directed, each edge appears twice with same weight


... Weighted Graph Representations21/34

Edge list representation with weights:

[Diagram:Pics/wgraphs/edgelistw-small.png]

Note: not very efficient for use in processing algorithms, unless weight-sorted


Minimum Spanning Trees


Minimum Spanning Trees23/34

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

Minimum spanning tree MST of graph G Problem: how to (efficiently) find MST for graph G?


... Minimum Spanning Trees24/34

Brute force optimisation solution:

typedef Graph MSTree; // an MST is a subgraph

MSTree findMST(Graph g)
{
   MSTree t, best;  float bestCost = MAXFLOAT;
   foreach (t in AllSpanningTrees(g)) {
      if (cost(t) < bestCost) {
         best = copy(t);  bestCost = cost(t);
      }
   }
   return best;
}

Not useful because #spanning trees is potentially large.


Exercise 5: Cost of MST25/34

The cost(t) function gives sum of edge weights in t.

Write a C function to compute this value, assuming


How about a function generate all spanning trees?


... Minimum Spanning Trees26/34

Simplifying assumptions:

If edges may have same weight:


Kruskal's Algorithm27/34

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

Critical operations:


... Kruskal's Algorithm28/34

Execution trace of Kruskal's algorithm:

[Diagram:Pics/wgraphs/kruskal-small.png]


Exercise 6: Kruskal's MST Algorithm29/34

Show how Kruskal's algorithm produces an MST on:

[Diagram:Pics/wgraphs/example-graph-small.png]


Implementation of Kruskal's Algorithm30/34

C-like description of algorithm:

MSTree kruskalFindMST(Graph g)
{
   Graph mst = newGraph(); //MST initially empty
   Edge eList[g->nV]; //sorted array of edges
   edges(eList, g->nE, g);
   sortEdgeList(eList, g->nE);
   int i;  Edge e;
   for (i = 0; mst->nE < g->nV-1; i++) {
      e = eList[i];
      insertE(mst, e);
      if (hasCycle(mst)) removeE(mst, e);
   }
   return mst;
}


... Implementation of Kruskal's Algorithm31/34

Rough cost analysis:

Possibilities for cycle checking:


Prim's Algorithm32/34

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

Critical operations:


... Prim's Algorithm33/34

Execution trace of Prim's algorithm:

[Diagram:Pics/wgraphs/prim-small.png]


Exercise 7: Prim's MST Algorithm34/34

Exercise: show how Prim's algorithm produces an MST on:

[Diagram:Pics/wgraphs/example-graph-small.png]


Produced: 8 Sep 2017