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

exp7

The document presents a C program implementing Dijkstra's algorithm for finding the single-source shortest path in a graph. It defines functions to calculate minimum distances and to perform the greedy algorithm, displaying the shortest distances from the source vertex to all other vertices. The program initializes a graph and outputs the shortest distances from the source vertex A.

Uploaded by

kesavat0001
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)
4 views3 pages

exp7

The document presents a C program implementing Dijkstra's algorithm for finding the single-source shortest path in a graph. It defines functions to calculate minimum distances and to perform the greedy algorithm, displaying the shortest distances from the source vertex to all other vertices. The program initializes a graph and outputs the shortest distances from the source vertex A.

Uploaded by

kesavat0001
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/ 3

single-source shortest path algorithm program

#include<stdio.h>

#include<limits.h>

#include<stdbool.h>

int min_dist(int[], bool[]);

void greedy_dijsktra(int[][6],int);

int min_dist(int dist[], bool visited[]){ // finding minimum dist

int minimum=INT_MAX,ind;

for(int k=0; k<6; k++) {

if(visited[k]==false && dist[k]<=minimum) {

minimum=dist[k];

ind=k;

return ind;

void greedy_dijsktra(int graph[6][6],int src){

int dist[6];

bool visited[6];

for(int k = 0; k<6; k++) {

dist[k] = INT_MAX;

visited[k] = false;

}
dist[src] = 0; // Source vertex dist is set 0

for(int k = 0; k<6; k++) {

int m=min_dist(dist,visited);

visited[m]=true;

for(int k = 0; k<6; k++) {

// updating the dist of neighbouring vertex

if(!visited[k] && graph[m][k] && dist[m]!=INT_MAX && dist[m]


+graph[m][k]<dist[k])

dist[k]=dist[m]+graph[m][k];

printf("Vertex\t\tdist from source vertex\n");

for(int k = 0; k<6; k++) {

char str=65+k;

printf("%c\t\t\t%d\n", str, dist[k]);

int main(){

int graph[6][6]= {

{0, 1, 2, 0, 0, 0},

{1, 0, 0, 5, 1, 0},

{2, 0, 0, 2, 3, 0},

{0, 5, 2, 0, 2, 2},
{0, 1, 3, 2, 0, 1},

{0, 0, 0, 2, 1, 0}

};

greedy_dijsktra(graph,0);

return 0;

Output
Vertex dist from source vertex
A 0
B 1
C 2
D 4
E 2
F 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