Professional Documents
Culture Documents
DIGITAL ASSIGNMENT - 1
1.Write a program to find the shortest path in a graph using either Prim’s
Algorithm or Kruskal’s Algorithm.
Prim’s Algorithm:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define V 5
return min_index;
}
int printMST(int parent[], int graph[V][V])
{
printf("Edge \tWeight\n");
for (int i = 1; i < V; i++)
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
}
int parent[V];
int key[V];
bool mstSet[V];
key[0] = 0;
parent[0] = -1;
printMST(parent, graph);
}
int main()
{
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
primMST(graph);
return 0;
}
Output:
Output:
The data structure which is being used in DFS is stack. The process is similar to
BFS algorithm. In DFS, the edges that leads to an unvisited node are called
discovery edges while the edges that leads to an already visited node are called
block edges.
Algorithm
o Step 1: SET STATUS = 1 (ready state) for each node in G
o Step 2: Push the starting node A on the stack and set its STATUS = 2
(waiting state)
o Step 3: Repeat Steps 4 and 5 until STACK is empty
o Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed
state)
o Step 5: Push on the stack all the neighbours of N that are in the ready
state (whose STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
o Step 6: EXIT
The algorithm of breadth first search is given below. The algorithm starts with
examining the node A and all of its neighbours. In the next step, the
neighbours of the nearest node of A are explored and process continues in the
further steps. The algorithm explores all neighbours of all the nodes and
ensures that each node is visited exactly once and no node is visited twice.
Algorithm
o Step 1: SET STATUS = 1 (ready state)
for each node in G
o Step 2: Enqueue the starting node A
and set its STATUS = 2
(waiting state)
o Step 3: Repeat Steps 4 and 5 until
QUEUE is empty
o Step 4: Dequeue a node N. Process it
and set its STATUS = 3
(processed state).
o Step 5: Enqueue all the neighbours of
N that are in the ready state
(whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
o Step 6: EXIT
Comparison:
Explanation:
Ford-Fulkerson method
Let's define one more thing. A residual capacity of an directed edge is the
capacity minus the flow. It should be noted that if there is a flow along some
directed edge (u,v)(u,v), than the reversed edge has capacity 0 and we can
define the flow of it as f((v,u))=−f((u,v))f((v,u))=−f((u,v)). This also defines the
residual capacity for all reversed edges. From all these edges we can create
a residual network, which is just a network with the same vertices and same
edges, but we use the residual capacities as capacities.
The Ford-Fulkerson method works as follows. First we set the flow of each
edge to zero. Then we look for an augmenting path from ss to tt. An
augmenting path is simple path in the residual graph, i.e. along the edges
whose residual capacity is positive. Is such a path is found, then we can add
increase the flow along these edges. We keep on searching for augmenting
paths and increasing the flow. Once there doesn't exists an augmenting path
any more, the flow is maximal.
Let us specify in more detail, what increasing the flow along an augmenting
path means. Let CC be the smallest residual capacity of the edges in the path.
Then we increase the flow in the following way: we
update f((u,v)) += Cf((u,v)) += C and f((v,u)) -= Cf((v,u)) -= C for every
edge (u,v)(u,v) in the path.
Edmonds-Karp algorithm
Edmonds-Karp algorithm is just an implementation of the Ford-Fulkerson
method that uses BFS for finding augmenting paths. The algorithm was first
published by Yefim Dinitz in 1970, and later independently published by Jack
Edmonds and Richard Karp in 1972.
The complexity can be given independently of the maximal flow. The algorithm
runs in O(VE2)O(VE2) time, even for irrational capacities. The intuition is, that
every time we find an augmenting path one of the edges becomes saturated,
and the distance from the edge to ss will be longer, if it appears later again in
an augmenting path. And the length of a simple paths is bounded by VV.
Implementation
The matrix capacity stores the capacity for every pair of vertices. adj is the
adjacency list of the undirected graph, since we have also to use the reversed
of directed edges when we are looking for augmenting paths.
The function maxflow will return the value of the maximal flow. During the
algorithm the matrix capacity will actually store the residual capacity of the
network. The value of the flow in each edge will actually not be stored, but it is
easy to extend the implementation - by using an additional matrix - to also
store the flow and return it.
int n;
vector<vector<int>> capacity;
vector<vector<int>> adj;
parent[s] = -2;
queue<pair<int, int>> q;
q.push({s, INF});
while (!q.empty()) {
q.pop();
parent[next] = cur;
if (next == t)
return new_flow;
q.push({next, new_flow});
return 0;
int flow = 0;
vector<int> parent(n);
int new_flow;
while (new_flow = bfs(s, t, parent)) {
flow += new_flow;
int cur = t;
while (cur != s) {
capacity[prev][cur] -= new_flow;
capacity[cur][prev] += new_flow;
cur = prev;
return flow;