0% found this document useful (0 votes)
105 views5 pages

Dijkstra'S Algorithm: (For Programming Based Labs)

This document describes implementing Dijkstra's algorithm to find the shortest paths in a graph with positive edge weights. It provides an overview of the algorithm, pseudocode for the algorithm, C++ code to implement it, and an analysis of the time and space complexity. The code takes a graph as input, initializes all distances to infinity and uses a priority queue to iteratively find the minimum distance vertex and update distances. It has a time complexity of O(E Log V) and space complexity of O(V).

Uploaded by

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

Dijkstra'S Algorithm: (For Programming Based Labs)

This document describes implementing Dijkstra's algorithm to find the shortest paths in a graph with positive edge weights. It provides an overview of the algorithm, pseudocode for the algorithm, C++ code to implement it, and an analysis of the time and space complexity. The code takes a graph as input, initializes all distances to infinity and uses a priority queue to iteratively find the minimum distance vertex and update distances. It has a time complexity of O(E Log V) and space complexity of O(V).

Uploaded by

Mohak Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy