0% found this document useful (0 votes)
20 views3 pages

Worksheet 8

The document describes applying Warshall's algorithm to find the shortest paths in a directed acyclic graph. It provides the steps of the algorithm, includes code to implement it, and shows sample output. The learning outcomes are to apply Warshall's algorithm to a directed acyclic graph and learn its implementation.

Uploaded by

Tarun saraswat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Worksheet 8

The document describes applying Warshall's algorithm to find the shortest paths in a directed acyclic graph. It provides the steps of the algorithm, includes code to implement it, and shows sample output. The learning outcomes are to apply Warshall's algorithm to a directed acyclic graph and learn its implementation.

Uploaded by

Tarun saraswat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Worksheet-8

Student Name: Tarun Sarasawat UID:22MCA20528

Branch: MCA Section/Group

Semester: Second Date of Performance: 30-3-2023

Subject Name: DAA Subject Code:22CAP-654

Q. Apply Warshall’s Algorithm by taking a Directed Acyclic Graph.

Aim:

Our aim is to apply Warshall’s using Direct Acyclic Graph.

Algorithm:

Step1: Initialize the solution matrix same as the input graph matrix as a first step.

Step2: Then update the solution matrix by considering all vertices as an intermediate
vertex.

Step3: The idea is to one by one pick all vertices and updates all shortest paths which
include the picked vertex as an intermediate vertex in the shortest path.

Step4: When we pick vertex number k as an intermediate vertex, we already have


considered vertices {0, 1, 2, .. k-1} as intermediate vertices.

Step5: For every pair (i, j) of the source and destination vertices respectively, there are
two possible cases.

Step6: k is not an intermediate vertex in shortest path from i to j. We keep the value of
dist[i][j] as it is.

Step7: k is an intermediate vertex in shortest path from i to j. We update the value of


dist[i][j] as dist[i][k] + dist[k][j] if dist[i][j] > dist[i][k] + dist[k][j]
Code for experiment/practical:

#include <bits/stdc++.h>
using namespace std;
#define V 4
#define INF 99999

void printSolution(int dist[][V]);


void floydWarshall(int dist[][V])
{

int i, j, k;
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (dist[i][j] > (dist[i][k] + dist[k][j])
&& (dist[k][j] != INF
&& dist[i][k] != INF))
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printSolution(dist);
}
void printSolution(int dist[][V])
{
cout << "The following matrix shows the shortest "
"distances"
" between every pair of vertices \n";
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
cout << "INF"
<< " ";
else
cout << dist[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int graph[V][V] = { { 0, 5, INF, 10 },
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
{ INF, INF, INF, 0 } };
floydWarshall(graph);
return 0;
}

Output:

Learning outcomes:

• I learnt how to apply warshall’s Algorithm using directed acyclic graph.


• Implementation of Warshall’s algorithm.

You might also like