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

C Program For Dijkstra & HAsh Table

The document contains two C programs: one implements Dijkstra's algorithm for finding the shortest path in a graph, and the other implements a static hash table with linear probing for insertion, searching, and displaying elements. The Dijkstra's algorithm program initializes a graph, computes distances from a source vertex, and prints the results. The hash table program allows users to insert values, search for values, and display the contents of the hash table.

Uploaded by

ashwini biradar
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)
14 views5 pages

C Program For Dijkstra & HAsh Table

The document contains two C programs: one implements Dijkstra's algorithm for finding the shortest path in a graph, and the other implements a static hash table with linear probing for insertion, searching, and displaying elements. The Dijkstra's algorithm program initializes a graph, computes distances from a source vertex, and prints the results. The hash table program allows users to insert values, search for values, and display the contents of the hash table.

Uploaded by

ashwini biradar
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/ 5

// C program for Dijkstra's single source shortest path algorithm.

#include <limits.h>
#include <stdbool.h>
#include <stdio.h>

#define max 9

int minDistance(int dist[], bool sptSet[])


{
int min = INT_MAX, min_index, v;

for (v = 0; v < max; v++)


if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;

return min_index;
}

void printSolution(int dist[])


{
printf("Vertex \t\t Distance from Source\n");
int i;
for (i = 0; i < max; i++)
printf("%d \t\t\t %d\n", i, dist[i]);
}

void dijkstra(int graph[max][ max], int src)


{
int dist[max];

bool sptSet[max];
int i,count,v;
for (i = 0; i < max; i++)
dist[i] = INT_MAX, sptSet[i] = false;

dist[src] = 0;

for (count = 0; count < max - 1; count++)


{
int u = minDistance(dist, sptSet);

sptSet[u] = true;

for (v = 0; v < max; v++)

if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u]


+ graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}

printSolution(dist);
}

int main()
{

int graph[max][ max] = {


{0, 4, 0, 0, 0, 0, 0, 8, 0}, {4, 0, 8, 0, 0, 0, 0, 11, 0}, {0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0}, {0, 0, 0, 9, 0, 10, 0, 0, 0}, {0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6}, {8, 11, 0, 0, 0, 0, 1, 0, 7}, {0, 0, 2, 0, 0, 0, 6, 7, 0}};

dijkstra(graph, 0);

return 0;
}
//Implementation of static hash table with Linear Probing in C
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10

int h[TABLE_SIZE]={NULL};

void insert()
{

int key,index,i,flag=0,hkey;
printf("\nenter a value to insert into hash table\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
{

index=(hkey+i)%TABLE_SIZE;

if(h[index] == NULL)
{
h[index]=key;
break;
}

if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");
}
void search()
{

int key,index,i,flag=0,hkey;
printf("\nenter search element\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE; i++)
{
index=(hkey+i)%TABLE_SIZE;
if(h[index]==key)
{
printf("value is found at index %d",index);
break;
}
}
if(i == TABLE_SIZE)
printf("\n value is not found\n");
}
void display()
{

int i;
printf("\nelements in the hash table are \n");

for(i=0;i< TABLE_SIZE; i++)


printf("\nat index %d \t value = %d",i,h[i]);

}
main()
{
int opt,i;
while(1)
{
printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");
scanf("%d",&opt);
switch(opt)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
exit(0);
}
}
}

You might also like