12. Graph Algorithms

Now we have the tools to see some graph algorithms.

12.1. Topological Sort

  • A topological sort is an ordering of nodes in a directed acyclic graph.

  • If there is a path from \(v_i\) to \(v_j\).

  • Then \(v_j\) appears after \(v_i\) in the ordering.

Example

  • Following an acyclic graph.

../../_images/acyclic_graph.png
  • An example of a topological ordering: \(v_1, v_2, v_5, v_4, v_7, v_3, v_6\).

Activity

  • Give another topological ordering.

12.1.1. A Simple Algorithm

  • We define the indegree of a node \(v\), as the number of edges \((u,v)\).

  • A simple algorithm to find topological ordering is:

    • Find any vertex with an indegree of 0.

    • Remove it along with its edges.

    • Apply the strategy with another vertex.

  • We obtain the following pseudocode:

 1void topsort()
 2{
 3    for (int counter = 0; counter < NUM_VERTICES; counter++){
 4        Vertex v = findNewVertexOfIndegreeZero();
 5        if (v == NOT_A_VERTEX){
 6            throw CycleFoundException();
 7        }
 8        v.topNum = counter;
 9        for each Vertex w adjacent to v{
10            w.indegree--;
11        }
12    }
13}
  • Where findNewVertexOfIndegreeZero() scans the array of vertices looking for a vertex with indegree 0.

  • If it returns NOT_A_VERTEX it indicates that the graph has a cycle.

  • The complexity of this algorithm \(O(|V|^2)\).

12.1.2. A Second Algorithm

  • The graph could be parsed, so checking each vertex could be a waste of time.

  • A second way to make a topological sort is to place every vertex of indegree 0 in a queue.

  • While the queue is not empty we remove one vertex.

  • All the adjacent vertices have their indegree decremented.

Following a pseudo-code of the algorithm:

 1void topsort()
 2{
 3    Queue<Vertex> q;
 4    int counter = 0;
 5
 6    q.makeEmpty( );
 7    for each Vertex v
 8        if( v.indegree == 0 )
 9            q.enqueue( v );
10
11    while( !q.isEmpty( ) )
12    {
13        Vertex v = q.dequeue( );
14        v.topNum = ++counter; // Assign next number
15        for each Vertex w adjacent to v
16            if( --w.indegree == 0 )
17                q.enqueue( w );
18    }
19
20    if( counter != NUM_VERTICES )
21        throw CycleFoundException();
22}
  • The running time of this algorithm is \(O(|E|+|V|)\).

Activity

  • Can you explain the running time?

  • Apply this algorithm to the previous graph.

12.2. Shortest-Path Algorithms

  • There are different shortest-path algorithms.

  • But, they have common properties:

    • The input is a weighted graph.

    • The weight for each edge \((v_i, v_j)\) is a cost \(c_{i,j}\) to traverse the edge.

    • The cost of a path \(v_1v_2\dots v_N\) is \(\sum_{i=1}^{N-1}c_{i,i+1}\).

../../_images/bfs.drawio1.png

Activity

  • What would be the cost of the path in an unweighted graph?

  • Consider the previous graph:

    • What is the cost of the path \(v_1, v_2, v_5, v_7\).

    • Do you have a shorter path from \(v_1\) to \(v_7\)?