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 49cee1d

Browse files
Adjacency matrix in Java
1 parent bb040eb commit 49cee1d

10 files changed

+858
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 adjacencyMatrixRepresentation;
7+
8+
public class Demo1DirectedGraph
9+
{
10+
public static void main(String [] args)
11+
{
12+
DirectedGraph g = new DirectedGraph();
13+
14+
g.insertVertex("Zero");
15+
g.insertVertex("One");
16+
g.insertVertex("Two");
17+
18+
g.insertEdge("One", "Two");
19+
g.insertEdge("Two", "Zero");
20+
21+
g.display();
22+
System.out.println("Vertices = " + g.vertices()+ ", Edges = " + g.edges());
23+
}
24+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 adjacencyMatrixRepresentation;
7+
8+
import java.util.Scanner;
9+
10+
public class Demo2DirectedGraph
11+
{
12+
public static void main(String [] args)
13+
{
14+
Scanner scan = new Scanner(System.in);
15+
DirectedGraph g = new DirectedGraph();
16+
int choice;
17+
String s1,s2;
18+
19+
while(true)
20+
{
21+
System.out.println("1.Display Adjacency Matrix");
22+
System.out.println("2.Insert a vertex");
23+
System.out.println("3.Insert an edge");
24+
System.out.println("4.Delete an edge");
25+
System.out.println("5.Display Indegree and outdegree of a vertex");
26+
System.out.println("6.Check if there is an edge between two vertices");
27+
System.out.println("7.Exit");
28+
System.out.print("Enter your choice : ");
29+
choice = scan.nextInt();
30+
if(choice==7)
31+
break;
32+
33+
switch(choice)
34+
{
35+
case 1:
36+
g.display();
37+
System.out.println("Vertices = " + g.vertices()+ ", Edges = " + g.edges());
38+
break;
39+
case 2:
40+
System.out.print("Insert a vertex : ");
41+
s1=scan.next();
42+
g.insertVertex(s1);
43+
break;
44+
case 3:
45+
System.out.print("Enter start and end vertices : ");
46+
s1=scan.next();
47+
s2=scan.next();
48+
g.insertEdge(s1,s2);
49+
break;
50+
case 4:
51+
System.out.print("Enter start and end vertices : ");
52+
s1=scan.next();
53+
s2=scan.next();
54+
g.deleteEdge(s1,s2);
55+
break;
56+
case 5:
57+
System.out.print("Enter a vertex : ");
58+
s1=scan.next();
59+
System.out.println("Indegree is : " + g.indegree(s1));
60+
System.out.println("Outdegree is : " + g.outdegree(s1));
61+
break;
62+
case 6:
63+
System.out.print("Enter two vertices : ");
64+
s1=scan.next();
65+
s2=scan.next();
66+
if(g.edgeExists(s1,s2))
67+
System.out.println("There is an edge from " + s1 + " to " + s2);
68+
else
69+
System.out.println("There is no edge from " + s1 + " to " + s2);
70+
71+
if(g.edgeExists(s2,s1))
72+
System.out.println("There is an edge from " + s2 + " to " + s1);
73+
else
74+
System.out.println("There is no edge from " + s2 + " to " + s1);
75+
76+
break;
77+
default:
78+
System.out.println("Wrong choice");
79+
break;
80+
}
81+
}
82+
}
83+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 adjacencyMatrixRepresentation;
7+
8+
import java.util.Scanner;
9+
10+
public class DemoDirectedWeighted
11+
{
12+
public static void main(String [] args)
13+
{
14+
Scanner scan = new Scanner(System.in);
15+
16+
DirectedWeightedGraph g = new DirectedWeightedGraph();
17+
18+
int choice,wt;
19+
String s1,s2;
20+
while(true)
21+
{
22+
System.out.println("1.Display Adjacency Matrix");
23+
System.out.println("2.Insert a vertex");
24+
System.out.println("3.Insert an edge");
25+
System.out.println("4.Delete an edge");
26+
System.out.println("5.Display Indegree and outdegree of a vertex");
27+
System.out.println("6.Check if there is an edge between two vertices");
28+
System.out.println("7.Exit");
29+
System.out.print("Enter your choice : ");
30+
choice = scan.nextInt();
31+
if(choice==7)
32+
break;
33+
34+
switch(choice)
35+
{
36+
case 1:
37+
g.display();
38+
System.out.println("Vertices = " + g.vertices()+ ", Edges = " + g.edges());
39+
break;
40+
case 2:
41+
System.out.print("Insert a vertex : ");
42+
s1=scan.next();
43+
g.insertVertex(s1);
44+
break;
45+
case 3:
46+
System.out.print("Enter start and end vertices : ");
47+
s1=scan.next();
48+
s2=scan.next();
49+
System.out.print("Enter weight : ");
50+
wt=scan.nextInt();
51+
g.insertEdge(s1,s2,wt);
52+
break;
53+
case 4:
54+
System.out.print("Enter start and end vertices : ");
55+
s1=scan.next();
56+
s2=scan.next();
57+
g.deleteEdge(s1,s2);
58+
break;
59+
case 5:
60+
System.out.print("Enter a vertex : ");
61+
s1=scan.next();
62+
System.out.println("Indegree is : " + g.indegree(s1));
63+
System.out.println("Outdegree is : " + g.outdegree(s1));
64+
break;
65+
case 6:
66+
System.out.print("Enter two vertices : ");
67+
s1=scan.next();
68+
s2=scan.next();
69+
if(g.edgeExists(s1,s2))
70+
System.out.println("There is an edge from " + s1 + " to " + s2);
71+
else
72+
System.out.println("There is no edge from " + s1 + " to " + s2);
73+
74+
if(g.edgeExists(s2,s1))
75+
System.out.println("There is an edge from " + s2 + " to " + s1);
76+
else
77+
System.out.println("There is no edge from " + s2 + " to " + s1);
78+
break;
79+
default:
80+
System.out.println("Wrong choice");
81+
break;
82+
}
83+
}
84+
}
85+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 adjacencyMatrixRepresentation;
7+
8+
import java.util.Scanner;
9+
10+
public class DemoUndirectedGraph
11+
{
12+
public static void main(String [] args)
13+
{
14+
Scanner scan = new Scanner(System.in);
15+
16+
UndirectedGraph g = new UndirectedGraph();
17+
18+
int choice;
19+
String s1,s2;
20+
21+
while(true)
22+
{
23+
System.out.println("1.Display Adjacency Matrix");
24+
System.out.println("2.Insert a vertex");
25+
System.out.println("3.Insert an edge");
26+
System.out.println("4.Delete an edge");
27+
System.out.println("5.Display Indegree and outdegree of a vertex");
28+
System.out.println("6.Check if there is an edge between two vertices");
29+
System.out.println("7.Exit");
30+
System.out.print("Enter your choice : ");
31+
choice = scan.nextInt();
32+
if(choice==7)
33+
break;
34+
35+
switch(choice)
36+
{
37+
case 1:
38+
g.display();
39+
System.out.println("Vertices = " + g.vertices()+ ", Edges = " + g.edges());
40+
break;
41+
case 2:
42+
System.out.print("Insert a vertex : ");
43+
s1=scan.next();
44+
g.insertVertex(s1);
45+
break;
46+
case 3:
47+
System.out.print("Enter start and end vertices : ");
48+
s1=scan.next();
49+
s2=scan.next();
50+
g.insertEdge(s1,s2);
51+
break;
52+
case 4:
53+
System.out.print("Enter start and end vertices : ");
54+
s1=scan.next();
55+
s2=scan.next();
56+
g.deleteEdge(s1,s2);
57+
break;
58+
case 5:
59+
System.out.print("Enter a vertex : ");
60+
s1=scan.next();
61+
System.out.println("Degree is : " + g.degree(s1));
62+
break;
63+
case 6:
64+
System.out.print("Enter two vertices : ");
65+
s1=scan.next();
66+
s2=scan.next();
67+
if(g.edgeExists(s1,s2))
68+
System.out.println("There is an edge from " + s1 + " to " + s2);
69+
else
70+
System.out.println("There is no edge from " + s1 + " to " + s2);
71+
break;
72+
default:
73+
System.out.println("Wrong choice");
74+
break;
75+
}
76+
}
77+
}
78+
79+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 adjacencyMatrixRepresentation;
7+
8+
import java.util.Scanner;
9+
10+
public class DemoUndirectedWeightedGraph
11+
{
12+
public static void main(String [] args)
13+
{
14+
Scanner scan = new Scanner(System.in);
15+
16+
UndirectedWeightedGraph g = new UndirectedWeightedGraph();
17+
18+
int choice,wt;
19+
String s1,s2;
20+
21+
while(true)
22+
{
23+
System.out.println("1.Display Adjacency Matrix");
24+
System.out.println("2.Insert a vertex");
25+
System.out.println("3.Insert an edge");
26+
System.out.println("4.Delete an edge");
27+
System.out.println("5.Display Indegree and outdegree of a vertex");
28+
System.out.println("6.Check if there is an edge between two vertices");
29+
System.out.println("7.Exit");
30+
System.out.print("Enter your choice : ");
31+
choice = scan.nextInt();
32+
if(choice==7)
33+
break;
34+
35+
switch(choice)
36+
{
37+
case 1:
38+
g.display();
39+
System.out.println("Vertices = " + g.vertices()+ ", Edges = " + g.edges());
40+
break;
41+
case 2:
42+
System.out.print("Insert a vertex : ");
43+
s1=scan.next();
44+
g.insertVertex(s1);
45+
break;
46+
case 3:
47+
System.out.print("Enter start and end vertices : ");
48+
s1=scan.next();
49+
s2=scan.next();
50+
System.out.print("Enter weight : ");
51+
wt=scan.nextInt();
52+
g.insertEdge(s1,s2,wt);
53+
break;
54+
case 4:
55+
System.out.print("Enter start and end vertices : ");
56+
s1=scan.next();
57+
s2=scan.next();
58+
g.deleteEdge(s1,s2);
59+
break;
60+
case 5:
61+
System.out.print("Enter a vertex : ");
62+
s1=scan.next();
63+
System.out.println("Degree is : " + g.degree(s1));
64+
break;
65+
case 6:
66+
System.out.print("Enter two vertices : ");
67+
s1=scan.next();
68+
s2=scan.next();
69+
if(g.edgeExists(s1,s2))
70+
System.out.println("There is an edge from " + s1 + " to " + s2);
71+
else
72+
System.out.println("There is no edge from " + s1 + " to " + s2);
73+
break;
74+
default:
75+
System.out.println("Wrong choice");
76+
break;
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
(0)

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