CSC393 Mar14

slide version

single file version

Contents

  1. Representing Graphs
  2. Adjacency Matrix for an Undirected Graph
  3. Adjacency List for an Undirected Graph
  4. Adjacency Matrix for a Directed Graph
  5. Adjacency List for a Directed Graph
  6. Topological Sort
  7. Sketch of Topological Sort Algorithm
  8. In-degrees of each vertex
  9. Data Structures for Topological Sort
  10. First Steps of the Algorithm
  11. Data Structures after first step; start of second step
  12. Data structures after second step
  13. Data Structures after all Vertices output.
  14. A Directed Graph with a Cycle
  15. Final Result for Algorithm on this Graph with a Cycle
  16. Shortest Distance from Selected Start: 1
  17. Shortest Distance: 2
  18. Shortest Distance: 3
  19. Shortest Distance: 4
  20. Shortest Distance: 5
  21. Shortest Distance: 6
  22. Shortest Distance: 7
  23. Shortest Distance: 7
  24. Shortest Distance: 8
  25. Shortest Distance: 9
  26. Shortest Distance: 10
  27. Shortest Distance: 11
  28. Shortest Distance: 12
  29. Shortest Distance: 13
  30. Shortest Distance: 14
  31. Implementation Issues
  32. Minimum Cost Spanning Tree
  33. Traveling Salesman Problem
  34. Example

Representing Graphs[1] [top]

There are two basic ways to represent directed graphs. Both ways also work for undirected graphs with no modification required (although some space could be eliminated).

Two Ways of Representing Graphs

 1. adjacency matrix. A |V| x |V| sized matrix, A

    For unweighted graph, 

	A[i][j] is 1 if there is an edge from vertex i to vetex j
                   0 otherwise.

   For weighted graph,

	A[i][j] is the weight of the edge if there is an edge from
                   vertex i to vetex j 
                   otherwise some chosen value not to be confused with
                   a weight (e.g., largest or smallest possible int
                   value)

   (Note for an undirected graph, the matrix A represents the same
    edge twice since A[i][j] will be equal to A[j][i].)



 2. adjacency list. An |V| sized array of lists. 

       A[i] = list of vertices j for which there is an edge from i to
              j.
  
    (Note for an undirected graph, every edge is represented twice.)

Adjacency Matrix for an Undirected Graph[2] [top]

               To
          A B C D E F G
        A 0 1 1 0 1 0 0
        B 1 0 0 1 0 0 1
        C 1 0 0 1 1 1 0
From    D 0 1 1 0 0 0 1
        E 1 0 1 0 0 1 0
        F 0 0 1 0 1 0 1
        G 0 1 0 1 0 1 0

Row k is the same as column k for each k. Why?

Adjacency List for an Undirected Graph[3] [top]

 From        To
    A: -> B,C,E
    B: -> A,D,G
    C: -> A,D,E,F
    D: -> B,C,G
    E: -> A,C,F
    F: -> C,E,G
    G: -> B,D,F

Each edge appears on 2 lists. Why?

Adjacency Matrix for a Directed Graph[4] [top]

               To
          A B C D E F G
        A 0 1 0 0 1 0 0
        B 0 0 0 0 0 0 0
        C 1 0 0 0 1 0 0
From    D 0 1 1 0 0 0 1
        E 0 0 0 0 0 0 0
        F 0 0 1 0 1 0 1
        G 0 1 0 0 0 0 0

Row 1 is not the same as column 1 for this directed graph. Why?

Adjacency List for a Directed Graph[5] [top]

    A: -> B,E
    B: -> -
    C: -> A,E
    D: -> B,C,G
    E: -> -
    F: -> C,E,G
    G: -> B

Each edge is on how many lists?

Topological Sort[6] [top]

This algorithm is applicable to a directed graph whose edges represent dependencies. It outputs the vertices in an order that respects the dependencies.

A directed graph

What node or nodes have no prerequisites?

These are the values that can be output first in topological sort order.

Sketch of Topological Sort Algorithm[7] [top]

The idea is to first output vertices with no dependencies; that is, with in-degree = 0.

Once a vertex has been output, we can remove it from the graph and decrease the in-degree of all its adjacent vertices.

Repeat the process until no more vertices remain with in-degree 0.

If all vertices have been output, then the output order is one of perhaps several possible topological sorted orders.

If all vertices have not been output, then a cycle exists and topological sort is not possible.

In-degrees of each vertex[8] [top]

The in-degrees of each vertex are crucial to the algorithm.

In-degrees of each
    vertex.

Data Structures for Topological Sort[9] [top]

First we have to represent the graph. Note that the in-degrees are not stored in the representation.

Next, the in-degrees will be computed in a separate data structure: degreeOf.

An additional separate data structure will be used to hold vertices that are ready to be output: zeroDegree

The graph can be represented by a HashMap whose keys are the graph vertices and whose values are LinkedLists of the adjacent vertices.

Using HashMap and
    LinkedList to represent the graph

First Steps of the Algorithm[10] [top]

Remove the first vertex
    from zeroDegree and process it.

Nothing added to zeroDegree

Data Structures after first step; start of second step[11] [top]

First vertex F is
    output. All vertices on its adjacency list have their in-degrees
    decremented in the degreeOf HashMap. None become 0, so nothing is
    added to the LinkedList zeroDegree.

Data structures after second step[12] [top]

After D is output, two new
    vertices have indegree 0 in HashMap degreeOf and so are added to
    zeroDegree.

Vertices C and G were added to zeroDegree since their degrees in the HashMap degreeOf just became 0.

The next node to be output will be C.

Data Structures after all Vertices output.[13] [top]

Final state. The LinkedList
    zeroDegree is empty. All vertices have been output in topological
    sort order. All vertices have 0 degree in the HashMap
    degreeOf. The graph data structures have not changed.

    Final state:
    - The LinkedList zeroDegree is empty.
    - All vertices have been output in topological sort order.
    - All vertices have 0 degree in the HashMap degreeOf.
    - The graph data structures have not changed.

A Directed Graph with a Cycle[14] [top]

This graph with the direction of one edge reversed now has a directed cycle.

The initial state of the data structures for topological sort are shown along with the graph representation.

A directed graph with a directed cycle.

Final Result for Algorithm on this Graph with a Cycle[15] [top]

Algorithm ends when zeroDegree is empty. 4 vertices not output

  - The algorithm ends when all previous elements in zeroDegree have been
    processed and the list is empty. 
  - However, 4 vertices have not been output.
  - The degrees of these 4 vertices in the degreeOf HashMap are not
    0. 
  - This means a cycle exists among those 4 nodes.

Shortest Distance from Selected Start: 1[16] [top]

Problem: Find the shortest path from V0 to each of the other vertices in this example weighted undirected graph:

The shortest distance from V0 to itself is clearly 0.

The shortest distance from V0 to the other vertices will be determined incrementally.

At each step we will discover one more vertex whose shortest distance from V0 is known as well as the path from V0.

Shortest Distance: 2[17] [top]

The vertices are in three groups:

  1. Vertices whose shortest distance from V0 is known
  2. vertices that are adjacent to some known vertex (the frontier vertices)
  3. all the other vertices (not known, not in the frontier)

Known: {V0}; Frontier: { V1, V2, V3}; Other: { V4, V5, V6 }

At each step, one frontier vertex can be added to the known vertices.

Which frontier vertex can become known in this example?

Shortest Distance: 3[18] [top]

Answer: Vertex V3 with a distance of 2.

The new known set: {V0, V3}

Why is there no shorter distance path, perhaps going through other vertices?

Shortest Distance: 4[19] [top]

Known: {V0, V3}; Frontier: { V1, V2, V4, V5}; Other: { V6 }

The distances for the known vertices are correct.

But the distances for the frontier vertices should be updated.

Why? Because, there may now be a shorter path that goes through the new known vertex V3.

The new known vertex V3 is adjacent to frontier vertices V2, V4, and V5.

Only the distances to these adjacent vertices V2, V4 and V5 may require updating.

Shortest Distance: 5[20] [top]

Vertex V1 was already in the frontier and had a preliminary distance of 3. But it is not adjacent to the new known vertex V3. So adding V3 doesn't (yet) give any new path to V1 and its distance stays the same.

For the vertices adjacent to V3

dist(V0, V2) = ?

dist(V0, V4) = ?

dist(V0, V5) = ?

Shortest Distance: 6[21] [top]

For the vertices adjacent to V3

dist(V0, V2) = dist(V0, V3) + dist(V3, V2) = 2 + 3 = 5

dist(V0, V4) = dist(V0, V3) + dist(V3, V4) = 2 + 8 = 10

dist(V0, V5) = dist(V0, V3) + dist(V3, V5) = 2 + 7 = 9

Shortest Distance: 7[22] [top]

After updating the distances the frontier the vertex with the smallest distance is V1

So V1 should be moved to the known set of vertices

Shortest Distance: 7[23] [top]

New know vertex: V1

Known vertices: {V0, V1, V3}

What is the frontier set now?

Which of these frontier vertices should have distances updated?

Shortest Distance: 8[24] [top]

New know vertex: V1

Known vertices: {V0, V1, V3}

Frontier set: {V2, V4, V5}

For the vertices V2 and V4 adjacent to V1, updated distances are:

dist(V0, V2) = dist(V0, V1) + dist(V1, V2) = 3 + 1 = 4

dist(V0, V4) = dist(V0, V1) + dist(V1, V4) = 3 + 6 = 9

Shortest Distance: 9[25] [top]

Which frontier vertex should be moved to known?

Shortest Distance: 10[26] [top]

Answer: V2

Shortest Distance: 11[27] [top]

New frontier set:

Only V4 adjacent to the new known vertex V2 was updated.

Frontier vertex V4 has smallest distance and should be moved to known.

Shortest Distance: 12[28] [top]

Shortest Distance: 13[29] [top]

For the frontier vertices V5 and V6 adjacent to V4, updated distances are:

dist(V0, V5) = dist(V0, V4) + dist(V4, V5) = 8 + 3 = 11

dist(V0, V6) = dist(V0, V4) + dist(V4, V6) = 8 + 2 = 10

But wait! dist(V0, V5) was already 9!

So we should not change this distance since the update through the new known vertex does not give a smaller value.

Shortest Distance: 14[30] [top]

Final shortest distances from V0 to each of the other vertices:

Implementation Issues[31] [top]

For a new known vertex, update the distance for its adjacent vertices.

Find the vertex with the minimum distance in the set of frontier vertices.

Delete the vertex with the minimum distance from the set of frontier vertices.

What data structure might be used?

Minimum Cost Spanning Tree[32] [top]

The shortest distance solution is an example of a greedy algorithm.

That is, at each step of the solution, we picked the smallest

Another problem that can be solved with a greedy algorithm: minimum cost spanning tree.

Given a weighted graph, find a subset of the edges that forms a tree and find one that has the minimum total weight (or cost).

When is a graph a tree?

Traveling Salesman Problem[33] [top]

Visit each vertex exactly once and return to the starting vertex.

Minimize total cost

Greedy algorithm doesn't work!

Example[34] [top]

Starting at vertex 1, a greedy algorithm for the traveling salesman problem below would begin:

      1 => 2 => 3
    

Then the next vertex can't be 1. So the next vertex would have to be either 4 or 5.

       1 => 2 => 3 => 4 => 5 => 1
cost:      1   +  10   + 100  + 1 + 40 = 152
    

or

       1 => 2 => 3 =>  5 => 4 => 1
cost:    1  + 10 + 100 + 1 +  30 = 142
    

But a better path would be to start with the second smallest cost (20) from vertex 1:

       1 => 3 => 2 => 5 => 4 => 1
cost:    20 + 10 + 15 + 1  + 30 = 76