-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP7_Graph_a.c
94 lines (71 loc) · 2.53 KB
/
P7_Graph_a.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_VERTICES 100
struct Graph {
int vertices;
int** adjacencyMatrix;
};
struct Graph* createGraph(int vertices) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->vertices = vertices;
graph->adjacencyMatrix = (int**)malloc(vertices * sizeof(int*));
for (int i = 0; i < vertices; i++) {
graph->adjacencyMatrix[i] = (int*)malloc(vertices * sizeof(int));
for (int j = 0; j < vertices; j++) {
graph->adjacencyMatrix[i][j] = 0;
}
}
return graph;
}
void addEdge(struct Graph* graph, int src, int dest, int weight) {
graph->adjacencyMatrix[src][dest] = weight;
graph->adjacencyMatrix[dest][src] = weight;
}
void dijkstra(struct Graph* graph, int src, int dest) {
int* distance = (int*)malloc(graph->vertices * sizeof(int));
int* visited = (int*)malloc(graph->vertices * sizeof(int));
for (int i = 0; i < graph->vertices; i++) {
distance[i] = INT_MAX;
visited[i] = 0;
}
distance[src] = 0;
for (int count = 0; count < graph->vertices - 1; count++) {
int minDistance = INT_MAX;
int minIndex = -1;
for (int v = 0; v < graph->vertices; v++) {
if (visited[v] == 0 && distance[v] <= minDistance) {
minDistance = distance[v];
minIndex = v;
}
}
visited[minIndex] = 1;
for (int v = 0; v < graph->vertices; v++) {
if (!visited[v] && graph->adjacencyMatrix[minIndex][v] &&
distance[minIndex] != INT_MAX &&
distance[minIndex] + graph->adjacencyMatrix[minIndex][v] < distance[v]) {
distance[v] = distance[minIndex] + graph->adjacencyMatrix[minIndex][v];
}
}
}
printf("Shortest distance from %d to %d: %d\n", src, dest, distance[dest]);
free(distance);
free(visited);
}
int main() {
int vertices, edges, src, dest, weight;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &vertices);
struct Graph* graph = createGraph(vertices);
printf("Enter the number of edges in the graph: ");
scanf("%d", &edges);
printf("Enter the edges (source destination weight):\n");
for (int i = 0; i < edges; i++) {
scanf("%d %d %d", &src, &dest, &weight);
addEdge(graph, src, dest, weight);
}
printf("Enter the source and destination nodes: ");
scanf("%d %d", &src, &dest);
dijkstra(graph, src, dest);
return 0;
}