forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jhonson Algorithm in C# (jainaman224#2626)
* Jhonson Algorithm in C# * Update Jhonson.cs
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// C# Program Implementing Jhonson Algorithm. | ||
|
||
using System; | ||
|
||
namespace ConsoleApp1 | ||
{ | ||
class Jhonson_Algorithm | ||
{ | ||
public static void Main(String[] args) | ||
{ | ||
int vert, edge, i, j, k, c; | ||
int INF = 999999; | ||
int[,] cost = new int[10, 10]; | ||
int[,] adj = new int[10, 10]; | ||
|
||
Console.WriteLine("Enter no of vertices: "); | ||
vert = Convert.ToInt32(Console.ReadLine()); | ||
Console.WriteLine("Enter no of Edges: "); | ||
edge = Convert.ToInt32(Console.ReadLine()); | ||
Console.WriteLine("Enter the EDGE cost: "); | ||
|
||
for (k = 1; k <= edge; k++) | ||
{ | ||
//take the input and store it into adj and cost matrix | ||
i = Convert.ToInt32(Console.ReadLine()); | ||
j = Convert.ToInt32(Console.ReadLine()); | ||
c = Convert.ToInt32(Console.ReadLine()); | ||
adj[i, j] = cost[i, j] = c; | ||
} | ||
|
||
for (i = 1; i <= vert; i++) | ||
for (j = 1; j <= vert; j++) | ||
{ | ||
if (adj[i, j] == 0 && i != j) | ||
//If its not a edge put infinity | ||
adj[i, j] = INF; | ||
} | ||
for (k = 1; k <= vert; k++) | ||
for (i = 1; i <= vert; i++) | ||
for (j = 1; j <= vert; j++) | ||
//Finding the minimum | ||
//find minimum path from i to j through k | ||
adj[i, j] = (adj[i, k] + adj[k, j]) > adj[i, j] ? adj[i, j] : (adj[i, k] + adj[k, j]) ; | ||
Console.WriteLine("The distance matrix of the graph.\n"); | ||
// Output the resultant matrix | ||
for (i = 1; i <= vert; i++) | ||
{ | ||
for (j = 1; j <= vert; j++) | ||
{ | ||
if (adj[i, j] != INF) | ||
Console.Write("{0} ", adj[i,j]); | ||
} | ||
Console.WriteLine(" "); | ||
} | ||
} | ||
} | ||
} | ||
/*Enter no of vertices: | ||
3 | ||
Enter no of Edges: | ||
5 | ||
Enter the EDGE cost: | ||
1 2 8 | ||
2 1 12 | ||
1 3 22 | ||
3 1 6 | ||
2 3 4 | ||
The distance matrix of the graph. | ||
0 8 12 | ||
10 0 4 | ||
6 14 0*/ |