-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.java
67 lines (67 loc) · 1.73 KB
/
Graph.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.*;
public class Graph{
Map<Integer, HashMap<Integer,Integer>> graph;
public Graph(){
this.graph=new HashMap<>();
/*
for(int i=0;i<=val;i++){
this.graph.put(val,new HashMap<>());
}*/
}
public void AddEdge(int v1,int v2,int w){
if(!graph.containsKey(v1)){
Addvertex(v1);
}
if(!graph.containsKey(v2)){
Addvertex(v2);
}
graph.get(v1).put(v2,w);
graph.get(v2).put(v1,w);
}
public void Addvertex(int v){
graph.put(v,new HashMap<>());
}
public void removeEdge(int v1,int v2){
if(graph.get(v1).containsKey(v2)){
graph.get(v1).remove(v2);
graph.get(v2).remove(v1);
}
else{
System.out.println("Edge Not Found!!!");
}
}
public void removeVertex(int v){
if(graph.containsKey(v)){
graph.remove(v);
for(int i:graph.keySet()){
if(graph.get(i).containsKey(v)){
graph.get(i).remove(v);
}
}
}
}
public int edge(){
int count=0;
for(int i:graph.keySet()){
count=count+graph.get(i).size();
}
return count/2;
}
public boolean hasPath(int v1,int v2){
if(graph.get(v1).containsKey(v2)){
return true;
}
for(int i:graph.get(v1).keySet()){
hasPath(i,v2);
}
return false;
}
public static void main(String args[]){
Graph graph=new Graph();
graph.AddEdge(1, 2, 0);
graph.AddEdge(1, 3, 0);
graph.AddEdge(2, 4, 0);
graph.AddEdge(4, 5, 0);
System.out.println(graph.edge());
}
}