Skip to content

Commit af9ac19

Browse files
committed
Added few graph algorithms
1 parent fe3ba6f commit af9ac19

File tree

7 files changed

+1139
-0
lines changed

7 files changed

+1139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// A C++ program for Bellman-Ford's single source
2+
// shortest path algorithm.
3+
#include <bits/stdc++.h>
4+
5+
// a structure to represent a weighted edge in graph
6+
struct Edge
7+
{
8+
int src, dest, weight;
9+
};
10+
11+
// a structure to represent a connected, directed and
12+
// weighted graph
13+
struct Graph
14+
{
15+
// V-> Number of vertices, E-> Number of edges
16+
int V, E;
17+
18+
// graph is represented as an array of edges.
19+
struct Edge* edge;
20+
};
21+
22+
// Creates a graph with V vertices and E edges
23+
struct Graph* createGraph(int V, int E)
24+
{
25+
struct Graph* graph = new Graph;
26+
graph->V = V;
27+
graph->E = E;
28+
graph->edge = new Edge[E];
29+
return graph;
30+
}
31+
32+
// A utility function used to print the solution
33+
void printArr(int dist[], int n)
34+
{
35+
printf("Vertex Distance from Source\n");
36+
for (int i = 0; i < n; ++i)
37+
printf("%d\t\t%d\n", i, dist[i]);
38+
}
39+
40+
// The main function that finds shortest distances from src to
41+
// all other vertices using Bellman-Ford algorithm. The function
42+
// also detects negative weight cycle
43+
void BellmanFord(struct Graph* graph, int src)
44+
{
45+
int V = graph->V;
46+
int E = graph->E;
47+
int dist[V];
48+
49+
// Step 1: Initialize distances from src to all other vertices
50+
// as INFINITE
51+
for (int i = 0; i < V; i++)
52+
dist[i] = INT_MAX;
53+
dist[src] = 0;
54+
55+
// Step 2: Relax all edges |V| - 1 times. A simple shortest
56+
// path from src to any other vertex can have at-most |V| - 1
57+
// edges
58+
for (int i = 1; i <= V-1; i++)
59+
{
60+
for (int j = 0; j < E; j++)
61+
{
62+
int u = graph->edge[j].src;
63+
int v = graph->edge[j].dest;
64+
int weight = graph->edge[j].weight;
65+
if (dist[u] != INT_MAX && dist[u] + weight < dist[v])
66+
dist[v] = dist[u] + weight;
67+
}
68+
}
69+
70+
// Step 3: check for negative-weight cycles. The above step
71+
// guarantees shortest distances if graph doesn't contain
72+
// negative weight cycle. If we get a shorter path, then there
73+
// is a cycle.
74+
for (int i = 0; i < E; i++)
75+
{
76+
int u = graph->edge[i].src;
77+
int v = graph->edge[i].dest;
78+
int weight = graph->edge[i].weight;
79+
if (dist[u] != INT_MAX && dist[u] + weight < dist[v])
80+
printf("Graph contains negative weight cycle");
81+
}
82+
83+
printArr(dist, V);
84+
85+
return;
86+
}
87+
88+
// Driver program to test above functions
89+
int main()
90+
{
91+
/* Let us create the graph given in above example */
92+
int V = 5; // Number of vertices in graph
93+
int E = 8; // Number of edges in graph
94+
struct Graph* graph = createGraph(V, E);
95+
96+
// add edge 0-1
97+
graph->edge[0].src = 0;
98+
graph->edge[0].dest = 1;
99+
graph->edge[0].weight = -1;
100+
101+
// add edge 0-2
102+
graph->edge[1].src = 0;
103+
graph->edge[1].dest = 2;
104+
graph->edge[1].weight = 4;
105+
106+
// add edge 1-2
107+
graph->edge[2].src = 1;
108+
graph->edge[2].dest = 2;
109+
graph->edge[2].weight = 3;
110+
111+
// add edge 1-3
112+
graph->edge[3].src = 1;
113+
graph->edge[3].dest = 3;
114+
graph->edge[3].weight = 2;
115+
116+
// add edge 1-4
117+
graph->edge[4].src = 1;
118+
graph->edge[4].dest = 4;
119+
graph->edge[4].weight = 2;
120+
121+
// add edge 3-2
122+
graph->edge[5].src = 3;
123+
graph->edge[5].dest = 2;
124+
graph->edge[5].weight = 5;
125+
126+
// add edge 3-1
127+
graph->edge[6].src = 3;
128+
graph->edge[6].dest = 1;
129+
graph->edge[6].weight = 1;
130+
131+
// add edge 4-3
132+
graph->edge[7].src = 4;
133+
graph->edge[7].dest = 3;
134+
graph->edge[7].weight = -3;
135+
136+
BellmanFord(graph, 0);
137+
138+
return 0;
139+
}

0 commit comments

Comments
 (0)