forked from Tiwarishashwat/Java-Plus-DSA-Placement-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph-2.java
More file actions
235 lines (210 loc) · 6.97 KB
/
graph-2.java
File metadata and controls
235 lines (210 loc) · 6.97 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Graph Lecture - 2 Code
// (Represent graph using adjacency list and adjacency matrix)
import java.util.*;
public class Graph {
class Pair{
int node;
int weight;
Pair(int n, int w){
node = n;
weight = w;
}
// (node, weight)
@Override
public String toString() {
return "(" +node +"," + weight + ")";
}
}
int adjMatrix[][];
List<List<Integer>> adjList;
List<List<Pair>> adjListWithWeight;
Graph(int nodes){
adjMatrix = new int[nodes][nodes]; //4*4 [0..]
adjList = new ArrayList<>();
adjListWithWeight = new ArrayList<>();
for(int i=0;i<nodes;i++){
adjList.add(new ArrayList<>());
adjListWithWeight.add(new ArrayList<>());
}
}
// ->[[0,2],[0,1],[2,0]]
public void addEdgesInMatrix(int edges[][], boolean isDirected){
for(int edge[] : edges){
int u = edge[0];
int v = edge[1];
//directed
if(isDirected){
adjMatrix[u][v] = 1;
}else{
//undirected
adjMatrix[u][v] = 1;
adjMatrix[v][u] = 1;
}
}
}
public void addEdgesWithWeightInMatrix(int edges[][], boolean isDirected){
for(int edge[] : edges){
int u = edge[0];
int v = edge[1];
int w = edge[2];
//directed
if(isDirected){
adjMatrix[u][v] = w;
}else{
//undirected
adjMatrix[u][v] = w;
adjMatrix[v][u] = w;
}
}
}
public void addEdgesInList(int edges[][], boolean isDirected){
for(int edge[] : edges){
int u = edge[0];
int v = edge[1];
//directed
if(isDirected){
adjList.get(u).add(v);
}else{
//undirected
adjList.get(u).add(v);
adjList.get(v).add(u);
}
}
}
public void addEdgesWithWeightInList(int edges[][], boolean isDirected){
for(int edge[] : edges){
int u = edge[0];
int v = edge[1];
int w = edge[2];
//directed
if(isDirected){
Pair pair = new Pair(v,w);
adjListWithWeight.get(u).add(pair);
}else{
//undirected
Pair pair1 = new Pair(v,w);
Pair pair2 = new Pair(u,w);
adjListWithWeight.get(u).add(pair1);
adjListWithWeight.get(v).add(pair2);
}
}
}
public void printMatrix(){
for(int i=0;i<adjMatrix.length;i++){
System.out.print("row "+i + "-> ");
for(int j=0;j<adjMatrix[i].length;j++){
System.out.print(adjMatrix[i][j] + ",");
}
System.out.println();
}
}
/*
0 -> [1,2,]
1 -> [0]
*/
public void printList(){
for(int i=0;i<adjList.size();i++){
System.out.print(i + " -> ");
System.out.print("[");
for(int j=0;j<adjList.get(i).size();j++){
System.out.print(adjList.get(i).get(j));
if(j!=adjList.get(i).size()-1){
System.out.print(", ");
}
}
System.out.print("]");
System.out.println();
}
}
public void printWeightedList(){
for(int i=0;i<adjListWithWeight.size();i++){
System.out.print(i + " -> ");
System.out.print("[");
for(int j=0;j<adjListWithWeight.get(i).size();j++){
System.out.print(adjListWithWeight.get(i).get(j));
if(j!=adjListWithWeight.get(i).size()-1){
System.out.print(", ");
}
}
System.out.print("]");
System.out.println();
}
}
public void findDegreeInUndirectedGraph(int edges[][], int nodes){
int degree[] = new int[nodes];
for(int edge[] : edges){
int u = edge[0];
int v = edge[1];
degree[u]++;
degree[v]++;
}
//print
for(int i=0;i<nodes;i++){
System.out.println("node -> " + i + " degree -> "+degree[i]);
}
}
public void findDegreeInDirectedGraph(int edges[][], int nodes){
int inDegree[] = new int[nodes];
int outDegree[] = new int[nodes];
for(int edge[] : edges){
int from = edge[0];
int to = edge[1];
inDegree[to]++;
outDegree[from]++;
}
//print
for(int i=0;i<nodes;i++){
System.out.print("node -> " + i + " in-degree -> "+inDegree[i] + " - ");
System.out.print("node -> " + i + " out-degree -> "+outDegree[i]);
System.out.println();
}
}
public static void main(String[] args) {
int edges[][] = {{0,2},{0,1},{1,3}};
int nodes = 4;
System.out.println("UnDirected Graph->");
Graph graph = new Graph(nodes);
graph.addEdgesInMatrix(edges,false);
graph.printMatrix();
System.out.println("Directed Graph->");
Graph graph1 = new Graph(nodes);
graph1.addEdgesInMatrix(edges,true);
graph1.printMatrix();
int edgesWithWeight[][] = {{0,2,10},{0,1,20},{1,3,30}};
System.out.println("Weighted UnDirected Graph->");
Graph graph2 = new Graph(nodes);
graph2.addEdgesWithWeightInMatrix(edgesWithWeight,false);
graph2.printMatrix();
System.out.println("Weighted Directed Graph->");
Graph graph3 = new Graph(nodes);
graph3.addEdgesWithWeightInMatrix(edgesWithWeight,true);
graph3.printMatrix();
System.out.println("UnDirected Graph->");
Graph graph4 = new Graph(nodes);
graph4.addEdgesInList(edges,false);
graph4.printList();
System.out.println("Directed Graph->");
Graph graph5 = new Graph(nodes);
graph5.addEdgesInList(edges,true);
graph5.printList();
System.out.println("Weighted UnDirected Graph->");
Graph graph6 = new Graph(nodes);
graph6.addEdgesWithWeightInList(edgesWithWeight,false);
graph6.printWeightedList();
System.out.println("Weighted Directed Graph->");
Graph graph7 = new Graph(nodes);
graph7.addEdgesWithWeightInList(edgesWithWeight,true);
graph7.printWeightedList();
// how to find degree of nodes in a graph
// --- undirected graph --- //
//find degree of nodes
Graph graph8 = new Graph(4);
System.out.println("UnDirected Graph");
graph8.findDegreeInUndirectedGraph(edges,4);
// --- directed graph --- //
//find indegree of nodes using edges
//find outdegree of nodes using edges
System.out.println("Directed Graph");
graph8.findDegreeInDirectedGraph(edges,4);
}
}