Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
### Graphs
- [x] [Terminology and Representations of Graphs](http://www.techiedelight.com/terminology-and-representations-of-graphs/)
- [x] Given a list of edges and tasked to build your own graph from the edges
- [x] Implement Dijkstra’s algorithm (implemented Weighted Graph to showcase this)
- [x] [Clone graph](https://leetcode.com/problems/clone-graph/)
### Matrix

### Trees
Expand Down Expand Up @@ -268,13 +270,11 @@


- Graphs
- [ ] Implement Dijkstra’s algorithm
- [ ] Implement Topological sort
- [ ] Implement Bellman-Ford algorithm
- [ ] Implement Floyd-Warshall algorithm
- [ ] Implement Prim’s algorithm
- [ ] Implement Kruskal’s algorithm
- [ ] [Clone graph](https://leetcode.com/problems/clone-graph/)
- [ ] [Course Schedule](https://leetcode.com/problems/course-schedule/)
- [ ] [Alien Dictionary](https://leetcode.com/problems/alien-dictionary/)
- [ ] [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)
Expand Down
35 changes: 35 additions & 0 deletions src/dataStructures/graphs/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dataStructures.graphs;

import java.util.ArrayList;
import java.util.List;

// LeetCode Node for examples
public class Node {
public int val;
public List<Node> neighbors;

public Node() {
val = 0;
neighbors = new ArrayList<>();
}

public Node(int _val) {
val = _val;
neighbors = new ArrayList<>();
}

public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}

// My addition: toString()

@Override
public String toString() {
return "Node{" +
"val=" + val +
", neighbors=" + neighbors +
'}';
}
}
86 changes: 86 additions & 0 deletions src/dataStructures/graphs/WeightedGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package dataStructures.graphs;

import java.util.*;

import dataStructures.graphs.CustomGraph.Vertex;

public class WeightedGraph {
private Map<Vertex, List<Edge>> adjVertices;

public static class Edge {
Vertex destination;
int weight;

public Edge(Vertex destination, int weight) {
this.destination = destination;
this.weight = weight;
}

@Override
public String toString() {
return "Edge{to=" + destination + ", weight=" + weight + '}';
}
}

public Map<Vertex, Integer> dijkstra(Vertex source) {
Map<Vertex, Integer> distances = new HashMap<>();
Map<Vertex, Vertex> previous = new HashMap<>();
PriorityQueue<VertexDistance> pq = new PriorityQueue<>(Comparator.comparingInt(vd -> vd.distance));

for (Vertex vertex : adjVertices.keySet()) {
distances.put(vertex, Integer.MAX_VALUE);
}
distances.put(source, 0);
pq.add(new VertexDistance(source, 0));

while (!pq.isEmpty()) {
VertexDistance current = pq.poll();
Vertex u = current.vertex;

for (Edge edge : adjVertices.getOrDefault(u, Collections.emptyList())) {
Vertex v = edge.destination;
int newDist = distances.get(u) + edge.weight;
if (newDist < distances.get(v)) {
distances.put(v, newDist);
previous.put(v, u);
pq.add(new VertexDistance(v, newDist));
}
}
}

return distances;
}

private static class VertexDistance {
Vertex vertex;
int distance;

public VertexDistance(Vertex vertex, int distance) {
this.vertex = vertex;
this.distance = distance;
}
}

// demo

public static void main(String[] args) {
Vertex a = new Vertex("A");
Vertex b = new Vertex("B");
Vertex c = new Vertex("C");
Vertex d = new Vertex("D");

WeightedGraph graph = new WeightedGraph();
graph.adjVertices = new HashMap<>();

graph.adjVertices.put(a, Arrays.asList(new Edge(b, 1), new Edge(c, 4)));
graph.adjVertices.put(b, Arrays.asList(new Edge(c, 2), new Edge(d, 5)));
graph.adjVertices.put(c, Arrays.asList(new Edge(d, 1)));
graph.adjVertices.put(d, new ArrayList<>());

Map<Vertex, Integer> distances = graph.dijkstra(a);
for (Map.Entry<Vertex, Integer> entry : distances.entrySet()) {
System.out.println("Distance from A to " + entry.getKey().label + " = " + entry.getValue());
}
}

}
42 changes: 42 additions & 0 deletions src/dataStructures/graphs/cloner/GraphCloner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dataStructures.graphs.cloner;

import dataStructures.graphs.Node;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GraphCloner {

public static void main(String[] args) {
Node n1 = new Node(1);
Node n2 = new Node(2);
n2.neighbors = List.of(n1);
Node root = new Node(0);
root.neighbors = List.of(n1, n2);
System.out.println("OG: " + root);
System.out.println("Clone: " + cloneGraph(root));
}

// DFS
public static Node cloneGraph(Node node) {
Map<Node, Node> map = new HashMap<>();

if (node == null) return null;

if (map.containsKey(node)) {
return map.get(node); // already cloned, use the previously cloned one
}

// Clone the node before recursion
Node clone = new Node(node.val); // create new node with same value
map.put(node, clone);

// Clone all the neighbors recursively
for (Node neighbor : node.neighbors) {
clone.neighbors.add(cloneGraph(neighbor));
}

return clone;
}
}