-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraphsImplementation.java
67 lines (56 loc) · 2.03 KB
/
GraphsImplementation.java
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
import java.util.*;
class GraphsImplementation{
static class Edge{
int src;
int dest;
Edge(int src, int dest){
this.src = src;
this.dest = dest;
}
}
static void initializeGraph(ArrayList<ArrayList> graph, int v){
for(int i = 0; i<v; i++){
graph.add(new ArrayList<Integer>());
}
}
static void addEdge(ArrayList<ArrayList> graph, Edge e){
if((0 <= e.src) && (e.src <= graph.size())){
graph.get(e.src).add(e.dest);
System.out.println("src : " + e.src + " dest: " +e.dest);
}
else{
System.out.println("Node out of graph bounds, enter a valid source.");
}
}
static void addEdgeUsingAdjacencyMatrix(int[][] adjMatrix, Edge e){
if(((0 <= e.src) && (e.src < adjMatrix.length))&& ((0 <= e.dest) && (e.dest < adjMatrix.length))){
adjMatrix[e.src][e.dest] = 1;
adjMatrix[e.dest][e.src] = 1; //Undirected graph
}
else{
System.out.println("Node out of graph bounds, enter a valid source.");
return;
}
for(int i =0; i<adjMatrix.length; i++){
for(int j = 0; j<adjMatrix.length; j++){
System.out.print(adjMatrix[i][j]);
}
System.out.println();
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the no.of vertices: ");
int v = input.nextInt();
System.out.println("Enter the no.of edges: ");
int e = input.nextInt();
//ArrayList<ArrayList> graph = new ArrayList<ArrayList>();
//initializeGraph(graph, v);
int[][] adjMatrix = new int[v][v];
for(int i = 0; i<e; i++){
System.out.println("Enter the edge (src, dest): ");
//addEdge(graph, new Edge(input.nextInt(), input.nextInt()));
addEdgeUsingAdjacencyMatrix(adjMatrix, new Edge(input.nextInt(), input.nextInt()));
}
}
}