Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b42614

Browse files
Path matrix in Java
1 parent 7abf247 commit 1b42614

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed

‎Graph/pathMatrix/Demo.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package pathMatrix;
7+
8+
public class Demo
9+
{
10+
public static void main(String [] args)
11+
{
12+
DirectedGraph g1 = new DirectedGraph();
13+
14+
g1.insertVertex("Zero");
15+
g1.insertVertex("One");
16+
g1.insertVertex("Two");
17+
g1.insertVertex("Three");
18+
19+
g1.insertEdge("Zero","One");
20+
g1.insertEdge("Zero","Three");
21+
g1.insertEdge("One","Two");
22+
g1.insertEdge("One","Three");
23+
g1.insertEdge("Three","Two");
24+
g1.findPathMatrix();
25+
26+
System.out.println();
27+
28+
DirectedGraph g2 = new DirectedGraph();
29+
30+
g2.insertVertex("Zero");
31+
g2.insertVertex("One");
32+
g2.insertVertex("Two");
33+
g2.insertVertex("Three");
34+
35+
g2.insertEdge("Zero","One");
36+
g2.insertEdge("Zero","Three");
37+
g2.insertEdge("One","Two");
38+
g2.insertEdge("One","Three");
39+
g2.insertEdge("Two","Zero");
40+
g2.insertEdge("Three","Two");
41+
g2.findPathMatrix();
42+
}
43+
}

‎Graph/pathMatrix/DirectedGraph.java

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package pathMatrix;
7+
8+
9+
public class DirectedGraph
10+
{
11+
public final int MAX_VERTICES = 30;
12+
13+
int n;
14+
int e;
15+
boolean [][] adj;
16+
Vertex [] vertexList;
17+
18+
public DirectedGraph()
19+
{
20+
adj = new boolean[MAX_VERTICES][MAX_VERTICES];
21+
vertexList = new Vertex[MAX_VERTICES];
22+
}
23+
24+
public void findPathMatrix()
25+
{
26+
int [][] x,adjp,temp;
27+
x = new int[n][n];
28+
adjp = new int[n][n];
29+
temp = new int[n][n];
30+
31+
for(int i=0; i<n; i++)
32+
for(int j=0; j<n; j++)
33+
x[i][j]= adjp[i][j] = adj[i][j] ? 1 : 0 ;
34+
35+
for(int p=2; p<=n; p++)
36+
{
37+
for(int i=0; i<n; i++)
38+
for(int j=0; j<n; j++)
39+
{
40+
temp[i][j] = 0;
41+
for(int k=0; k<n; k++)
42+
temp[i][j] = temp[i][j] + adjp[i][k] * (adj[k][j]?1:0);
43+
}
44+
45+
for(int i=0; i<n; i++)
46+
for(int j=0; j<n; j++)
47+
adjp[i][j] = temp[i][j];
48+
49+
for(int i=0; i<n; i++)
50+
for(int j=0; j<n; j++)
51+
x[i][j] = x[i][j] + adjp[i][j];
52+
}
53+
54+
//Display X
55+
for(int i=0; i<n; i++)
56+
{
57+
for (int j=0; j<n; j++)
58+
System.out.print(x[i][j] + " ");
59+
System.out.println();
60+
}
61+
System.out.println();
62+
63+
boolean [][] path;
64+
path = new boolean[n][n];
65+
for(int i=0; i<n; i++)
66+
for(int j=0; j<n; j++)
67+
if (x[i][j] == 0 )
68+
path[i][j] = false;
69+
else
70+
path[i][j] = true;
71+
72+
//Display Path Matrix
73+
for(int i=0; i<n; i++)
74+
{
75+
for (int j=0; j<n; j++)
76+
if (path[i][j])
77+
System.out.print("1 ");
78+
else
79+
System.out.print("0 ");
80+
System.out.println();
81+
}
82+
}
83+
84+
85+
public void display()
86+
{
87+
for(int i=0; i<n; i++)
88+
{
89+
for(int j=0; j<n; j++)
90+
if (adj[i][j])
91+
System.out.print("1 ");
92+
else
93+
System.out.print("0 ");
94+
System.out.println();
95+
}
96+
}
97+
98+
private int getIndex(String s)
99+
{
100+
for(int i=0; i<n; i++)
101+
if(s.equals(vertexList[i].name))
102+
return i;
103+
throw new RuntimeException("Invalid Vertex");
104+
}
105+
106+
public void insertVertex(String name)
107+
{
108+
vertexList[n++] = new Vertex(name);
109+
}
110+
111+
112+
/*Insert an edge (s1,s2) */
113+
public void insertEdge(String s1, String s2)
114+
{
115+
int u = getIndex(s1);
116+
int v = getIndex(s2);
117+
if(u==v)
118+
throw new IllegalArgumentException("Not a valid edge");
119+
if(adj[u][v] == true)
120+
System.out.print("Edge already present");
121+
else
122+
{
123+
adj[u][v]=true;
124+
e++;
125+
}
126+
}
127+
128+
129+
}
130+

‎Graph/pathMatrix/Vertex.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package pathMatrix;
7+
8+
public class Vertex
9+
{
10+
String name;
11+
12+
Vertex(String name)
13+
{
14+
this.name = name;
15+
}
16+
public String toString()
17+
{
18+
return name;
19+
}
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /