Skip to content

Commit dec910c

Browse files
committed
Graph implementation
1 parent ee3c6de commit dec910c

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
- [x] [Sort HashMap by value](https://www.digitalocean.com/community/tutorials/java-programming-interview-questions#24-write-a-java-program-that-sorts-hashmap-by-value)
127127

128128
### Graphs
129-
129+
- [x] Given a list of edges and tasked to build your own graph from the edges
130130
### Matrix
131131

132132
### Trees
@@ -267,7 +267,6 @@
267267

268268

269269
- Graphs
270-
- [ ] Given a list of edges and tasked to build your own graph from the edges
271270
- [ ] Implement Dijkstra’s algorithm
272271
- [ ] Implement Topological sort
273272
- [ ] Implement Bellman-Ford algorithm
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package dataStructures.graphs;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.Objects;
7+
8+
public class CustomGraph {
9+
10+
public static void main(String[] args) {
11+
CustomGraph customGraph = new CustomGraph();
12+
Map<Vertex, List<Vertex>> map = new HashMap<>();
13+
map.put(new Vertex("name"), List.of());
14+
map.put(new Vertex("name2"), List.of(new Vertex("name")));
15+
customGraph.setAdjVertices(map);
16+
System.out.println(customGraph);
17+
}
18+
19+
//
20+
private Map<Vertex, List<Vertex>> adjVertices;
21+
22+
public CustomGraph() {
23+
}
24+
25+
public CustomGraph(Map<Vertex, List<Vertex>> adjVertices) {
26+
this.adjVertices = adjVertices;
27+
}
28+
29+
public Map<Vertex, List<Vertex>> getAdjVertices() {
30+
return adjVertices;
31+
}
32+
33+
public void setAdjVertices(Map<Vertex, List<Vertex>> adjVertices) {
34+
this.adjVertices = adjVertices;
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "Graph {" +
40+
"vertices=" + adjVertices +
41+
'}';
42+
}
43+
44+
public static class Vertex {
45+
String label;
46+
Vertex(String label) {
47+
this.label = label;
48+
}
49+
50+
@Override
51+
public boolean equals(Object o) {
52+
if (o == null || getClass() != o.getClass())
53+
return false;
54+
Vertex vertex = (Vertex) o;
55+
return Objects.equals(label, vertex.label);
56+
}
57+
58+
@Override
59+
public int hashCode() {
60+
return Objects.hashCode(label);
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "Vertex {" +
66+
"label='" + label + '\'' +
67+
'}';
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)