Skip to content

Commit 106b849

Browse files
committed
Graph data structure implementation from scratch, using HashMap
1 parent 4ea8be8 commit 106b849

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package section20_Graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
6+
public class Graph {
7+
8+
private class Vertex {
9+
// stores all neighbors of particular Node
10+
HashMap<String, Integer> nbrs = new HashMap<>();
11+
}
12+
13+
HashMap<String, Vertex> vertices;
14+
15+
public Graph() {
16+
vertices = new HashMap<>();
17+
}
18+
19+
public int numVertex() {
20+
return this.vertices.size();
21+
}
22+
23+
public boolean containsVertex(String vname) {
24+
return this.vertices.containsKey(vname);
25+
}
26+
27+
public void addVertex(String vname) {
28+
Vertex vtx = new Vertex();
29+
this.vertices.put(vname, vtx);
30+
}
31+
32+
public void removeVertex(String vname) {
33+
if (this.vertices.get(vname) == null) {
34+
return;
35+
}
36+
Vertex vtx = this.vertices.get(vname);
37+
38+
ArrayList<String> allNeighborsKeys = new ArrayList<>(vtx.nbrs.keySet());
39+
// removing vname from their neighbors first
40+
for (String key : allNeighborsKeys) {
41+
Vertex nbrVertex = this.vertices.get(key);
42+
nbrVertex.nbrs.remove(vname);
43+
}
44+
// now removing neighbors form Graph
45+
this.vertices.remove(vname);
46+
}
47+
48+
public int numEdges() {
49+
int count = 0;
50+
ArrayList<String> keys = new ArrayList<>(this.vertices.keySet());
51+
for (String key : keys) {
52+
Vertex vtx = vertices.get(key);
53+
count += vtx.nbrs.size();
54+
}
55+
return count / 2;
56+
}
57+
58+
public boolean containsEdge(String vname1, String vname2) {
59+
Vertex firstNode = vertices.get(vname1);
60+
Vertex secondNode = vertices.get(vname2);
61+
62+
if (firstNode == null || secondNode == null || !firstNode.nbrs.containsKey(vname2)) {
63+
return false;
64+
}
65+
return true;
66+
}
67+
68+
public void addEdge(String vname1, String vname2, int cost) {
69+
Vertex vertex1 = vertices.get(vname1);
70+
Vertex vertex2 = vertices.get(vname2);
71+
72+
if (vertex1 == null || vertex2 == null || vertex1.nbrs.containsKey(vname2)) {
73+
return;
74+
}
75+
vertex1.nbrs.put(vname2, cost);
76+
vertex2.nbrs.put(vname1, cost);
77+
}
78+
79+
public void removeEdge(String vname1, String vname2) {
80+
Vertex vertex1 = vertices.get(vname1);
81+
Vertex vertex2 = vertices.get(vname2);
82+
83+
if (vertex1 == null || vertex2 == null || !vertex1.nbrs.containsKey(vname2)) {
84+
return;
85+
}
86+
vertex1.nbrs.remove(vname2);
87+
vertex2.nbrs.remove(vname1);
88+
}
89+
90+
public void dispaly() {
91+
System.out.println("--------------------------");
92+
ArrayList<String> allKeys = new ArrayList<>(this.vertices.keySet());
93+
for (String key : allKeys) {
94+
Vertex vtx = this.vertices.get(key);
95+
System.out.println(key + ": " + vtx.nbrs);
96+
}
97+
System.out.println("--------------------------");
98+
}
99+
}

0 commit comments

Comments
Β (0)