-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
48 lines (41 loc) · 1.21 KB
/
Copy pathGraph.java
File metadata and controls
48 lines (41 loc) · 1.21 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
package test;
import java.util.LinkedList;
public class Graph {
int V; // num of vertices
LinkedList<Edge>[] adj; // adjacency list of nodes
Graph(int edge){
V = edge;
adj=new LinkedList[V];
for(int i=0; i< V; i++){
adj[i] = new LinkedList<>(); // vertex and his neighbors
}
}
Graph(Graph graph){
this.V = graph.V;
adj = new LinkedList[this.V];
for(int i=0; i< V; i++){
adj[i] = new LinkedList<>(graph.adj[i]);
}
}
public Edge srcEdge(int src, int dst){ // look for edge by it source vertex
for(int i=0; i<V; i++)
{
for(int j=0; j<adj[i].size(); j++)
{
if(adj[i].get(j).destination == dst && src == i)
return adj[i].get(j); // edge with src and dst provided was found!
}
}
return null; // not found
}
public void printGraph()
{
for (int i=0; i< this.adj.length; i++)
{
for(int j=0; j<this.adj[i].size(); j++)
{
System.out.println(i+ "<->"+ this.adj[i].get(j).destination + "-"+"weight: "+ this.adj[i].get(j).weight );
}
}
}
}