Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

DIJKSTRA’S ALGORITHM

Student Name: ANSHUL JANGIR UID: 18BCS1408


Branch: BE - CSE Section/Group: CSE - 3B
Semester: 5th
Subject Name : DAA Lab Subject Code: CSP - 309

1. Aim/Overview of the practical:


Code and analyze to find shortest paths in a graph with positive edge weights using Dijkstra’s
algorithm.

2. Task to be done/ Which logistics used:


Find shortest path using Dijkstra Algoritm
Program Written in : C++ (IDE - VS Code)

3. Algorithm/Flowchart (For programming based labs):

function dijkstra(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
If V != S, add V to Priority Queue Q
distance[S] <- 0

while Q IS NOT EMPTY


U <- Extract MIN from Q
for each unvisited neighbour V of U
tempDistance <- distance[U] + edge_weight(U, V)
if tempDistance < distance[V]
distance[V] <- tempDistance
previous[V] <- U
return distance[], previous[]

4. Steps for experiment/practical/Code:

#include<iostream>
#include<climits>
using namespace std;
int findMinVertex(int* distance, bool* visited, int n)
{
    int minVertex = -1;
    for(int i = 0; i < n; i++)
    {
        if(!visited[i] && (minVertex == -1 ||  distance[i] < distance[minVertex]))
        {
            minVertex = i;
        }
    }
    return minVertex;
}
void dijkstra(int** edges, int n)
{
    int* distance = new int[n];
    bool* visited = new bool[n];
    for(int i = 0; i < n; i++)
    {
        distance[i] = INT_MAX;
        visited[i] = false;
    }
    distance[0] = 0;
    for(int i = 0; i < n - 1; i++)
    {
        int minVertex = findMinVertex(distance, visited, n);
        visited[minVertex] = true;
        for(int j = 0; j < n; j++)
        {   
            if(edges[minVertex][j] != 0 && !visited[j])
            {
                int dist = distance[minVertex] + edges[minVertex][j];
                if(dist < distance[j])
                {
                    distance[j] = dist;
                }
            }
        }
    }
    for(int i = 0; i < n; i++)
    {
        cout << i << " " << distance[i] << endl;
    }
    delete [] visited;
    delete [] distance; 
}

int main()
{
    cout << "Enter\n";
    int n;
    int e;
    cin >> n >> e;
    int** edges = new int*[n];
    for (int i = 0; i < n; i++) 
    {
        edges[i] = new int[n];
        for (int j = 0; j < n; j++) 
        {
            edges[i][j] = 0;
        }
    }
    for (int i = 0; i < e; i++) 
    {
        int f, s, weight;
        cin >> f >> s >> weight;
        edges[f][s] = weight;
        edges[s][f] = weight;
    }
    cout << endl;
    dijkstra(edges, n);
    for (int i = 0; i < n; i++) 
    {
        delete [] edges[i];
    }
    delete [] edges;

  return 0;
}
5. Observations/Discussions/ Complexity Analysis:
Time Complexity: O(E Log V)
where, E is the number of edges and V is the number of vertices.
Space Complexity: O(V)

6. Result/Output/Writing Summary:

Learning outcomes (What I have learnt):


1. Learnt about multiple ways to solve a problem.
2. Learnt about Dijsktra Algorithm.
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):
Sr. No. Parameters Marks Obtained Maximum Marks
1.
2.
3.

You might also like