Directed Acyclic Graph (DAG) | DAA

Directed Acyclic Graph (DAG)

DAG, here directed means that each edge has an arrow denoting that the edge can be traversed in only that particular direction. Acyclic means that the graph has no cycles, i.e., starting at one node, you can never end up at the same node. DAG can be used to find the shortest path from a given source node to all other nodes. To find the shortest path by using DAG, first of all sort the vertices of the graph topologically and then relax the vertices in topological order.



Example:

Directed Acyclie Graph (DAG) | DAA

Step 1: Sort the vertices of the graph topologically
Directed Acyclie Graph (DAG) | DAA

Step 2: Relax from S
Directed Acyclie Graph (DAG) | DAA

Step 3: Relax from C
Directed Acyclie Graph (DAG) | DAA

Step 4: Relax from A
Directed Acyclie Graph (DAG) | DAA

Step5: Relax from B
Directed Acyclie Graph (DAG) | DAA

Step6: Relax from D
Directed Acyclie Graph (DAG) | DAA


Algorithm

DagSP(G,w,s)
{
Topologically Sort the vertices of G
for each vertex v belongs to V
do d[v] = ?
d[s] = 0
for each vertex u, taken in topologically sorted order
do for each vertex v adjacent to u
do if d[v] > d[u] + w(u,v)
then d[v] = d[u] + w(u,v)
}

Analysis:

In the above algorithm, the topological sort can be done in O(V+E) time (Since this is similar to DPS! see a book.).The first for loop block lakes O(V) time. In the case of the second for loop, it executes in O(V2) Time so the total running time is O(V2). The aggregate analysis gives us the running time O(E+V).


Comments

Popular posts from this blog

C Program for SCAN Disk Scheduling Algorithm | C Programming

C program to Find Cartesian Product of Two Sets | C programming

C Program To Check The String Is Valid Identifier Or Not | C Programming