Skip to content

Commit 5096d8d

Browse files
author
Michael Ho
committed
Completed graph algorithms session 1
1 parent bd05fdb commit 5096d8d

File tree

12 files changed

+491
-156
lines changed

12 files changed

+491
-156
lines changed

.idea/workspace.xml

+152-111
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Difficulty | 🍰 | 🌰 🌰 | 🌰 | --- | ---
2121
**Divide and Conquer** | **[Lesson 1](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC1.java)** | **[Lesson 2](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC2.java)** | **[Lesson 3](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC3.java)** | **[Lesson 4](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC4.java)** | **[Lesson 5](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC5.java)**
2222
Difficulty | 🍰 | 🌰 | 🍰 | 🌰 🌰 🌰 | 🌰 🌰 🌰
2323
**Graph Algorithms** | **[Lesson 1](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC1.java)** | **[Lesson 2](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC2.java)** | **[Lesson 3](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC3.java)** | **[Lesson 4](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC4.java)** | **---**
24-
Difficulty | --- | --- | --- | --- | ---
24+
Difficulty | 🌰 | --- | --- | --- | ---
2525

2626
#### [Dynamic Programming](https://docs.google.com/document/d/1vziO8Enan327BmAeR9yAxNgySxRCF01qZlb6c-i627E/edit?usp=sharing)
2727
##### Lesson 1 - [source code](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DynamicProgramming/DP1.java)
@@ -71,6 +71,12 @@ Difficulty | --- | --- | --- | --- | ---
7171
##### Lesson 5 - [source code](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC5.java)
7272
1. Polynomial Multiplication using FFT
7373

74+
#### [Graph Algorothms](https://docs.google.com/document/d/1EgGK90bY5BT35MNbeNR9dn4kV_yJ7k4hHerCz7nPHbE/edit?usp=sharing)
75+
##### Lesson 1 - [source code](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/GraphAlgorithms/GR1.java)
76+
1. Depth First Search in Graph
77+
1. Topological Sort
78+
1. Find Strongly Connected Components in Graph
79+
7480
### Credits:
7581
- [Udacity Course - Introducation to Graduate Algorithms](https://classroom.udacity.com/courses/ud401)
7682
- [Geeks for Geeks](https://www.geeksforgeeks.org/)

src/com/michaelho/DataObjects/Graph.java

+98-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.michaelho.DataObjects;
22

3+
import com.michaelho.Main;
4+
35
import java.util.ArrayList;
4-
import java.util.LinkedList;
56
import java.util.List;
67

78
/**
@@ -38,8 +39,101 @@ public void addEdge(int src, int dest, int weight) {
3839
this.edges.add(edge);
3940
}
4041

41-
// Adds an edge to an undirected graph
42-
public ArrayList<Edge> getEdgesList() {
42+
public List<Edge> getEdgesList() {
4343
return edges;
4444
}
45-
}
45+
46+
public List<Edge> getAdjacencyList(int v) {
47+
List<Edge> adjacencyList = new ArrayList<>();
48+
for (int i = 0; i < edges.size(); i++) {
49+
if (edges.get(i).src.val == v) {
50+
adjacencyList.add(edges.get(i));
51+
}
52+
}
53+
return adjacencyList;
54+
}
55+
56+
public static Graph createGraph(boolean negative) {
57+
int V = 5; // Number of vertices in graph
58+
int E = 8; // Number of edges in graph
59+
60+
Graph graph = new Graph(V, E);
61+
62+
// add edge 0-1
63+
graph.addEdge(0, 1, negative ? -1 : 1);
64+
65+
// add edge 0-2
66+
graph.addEdge(0, 2, 4);
67+
68+
// add edge 1-2
69+
graph.addEdge(1, 2, 3);
70+
71+
// add edge 1-3
72+
graph.addEdge(1, 3, 2);
73+
74+
// add edge 1-4
75+
graph.addEdge(1, 4, 2);
76+
77+
// add edge 3-2
78+
graph.addEdge(3, 2, 5);
79+
80+
// add edge 3-1
81+
graph.addEdge(3, 1, 1);
82+
83+
// add edge 4-3
84+
graph.addEdge(4, 3, negative ? -3 : 3);
85+
86+
return graph;
87+
}
88+
89+
public static Graph getGraphForTopologicalSort() {
90+
int V = 6; // Number of vertices in graph
91+
int E = 6; // Number of edges in graph
92+
93+
Graph graph = new Graph(V, E);
94+
95+
// add edge 5-2
96+
graph.addEdge(5, 2, 1);
97+
98+
// add edge 5-0
99+
graph.addEdge(5, 0, 1);
100+
101+
// add edge 4-0
102+
graph.addEdge(4, 0, 1);
103+
104+
// add edge 4-1
105+
graph.addEdge(4, 1, 1);
106+
107+
// add edge 2-3
108+
graph.addEdge(2, 3, 1);
109+
110+
// add edge 3-1
111+
graph.addEdge(3, 1, 1);
112+
113+
return graph;
114+
}
115+
116+
public static Graph getGraphForFindingSCC() {
117+
int V = 5; // Number of vertices in graph
118+
int E = 5; // Number of edges in graph
119+
120+
Graph graph = new Graph(V, E);
121+
122+
// add edge 1-0
123+
graph.addEdge(1, 0, 1);
124+
125+
// add edge 0-2
126+
graph.addEdge(0, 2, 1);
127+
128+
// add edge 0-3
129+
graph.addEdge(0, 3, 1);
130+
131+
// add edge 2-1
132+
graph.addEdge(2, 1, 1);
133+
134+
// add edge 3-4
135+
graph.addEdge(3, 4, 1);
136+
137+
return graph;
138+
}
139+
}

src/com/michaelho/DataObjects/Node.java

+17
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,21 @@ public class Node {
77
Node(int d) {
88
val = d;
99
}
10+
11+
@Override
12+
public boolean equals(Object o) {
13+
// self check
14+
if (this == o)
15+
return true;
16+
// null check
17+
if (o == null)
18+
return false;
19+
// type check and cast
20+
if (getClass() != o.getClass())
21+
return false;
22+
Node node = (Node) o;
23+
24+
// field comparison
25+
return node.val == this.val;
26+
}
1027
}

src/com/michaelho/DynamicProgramming/DP1.java

-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ private int dpFindLongestLength(String s1, String s2, int idx1, int idx2) {
8989
if (idx1 < 0 || idx2 < 0) {
9090
return 0;
9191
}
92-
9392
if (s1.charAt(idx1) == s2.charAt(idx2))
9493
return 1 + dpFindLongestLength(s1, s2, idx1 - 1, idx2 - 1);
9594
else

src/com/michaelho/DynamicProgramming/DP2.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ int knapSackSol1(int[] wt, int[] val, int idx, int w) {
4040
* The runtime is O(NW) where N is the number of objects and W is the weight limit
4141
* of the knapsack. The runtime is the same as previous version of knapsack problem.
4242
*
43-
* @param wt The array represents the values of input objects.
44-
* @param val The array represents the weights of input objects.
43+
* @param wt The array represents the weights of input objects.
44+
* @param val The array represents the values of input objects.
4545
* @param w The total weight allowed remaining.
4646
* @return int The results of calculation.
4747
* */

src/com/michaelho/DynamicProgramming/DP3.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.michaelho.DataObjects.Graph;
55

66
import java.util.ArrayList;
7+
import java.util.List;
78

89
/**
910
* The DP3 class explores a set of dynamic programming questions and solutions such as Dijstra's algorithm,
@@ -56,7 +57,7 @@ int[] shortestPath(Graph graph, int src) {
5657
sptSet[u] = true;
5758

5859
// Update dist value of the adjacent vertices of the picked vertex.
59-
ArrayList<Edge> edges = graph.getEdgesList();
60+
List<Edge> edges = graph.getEdgesList();
6061
for (int v = 0; v < V; v++)
6162
// Update dist[v] only if it is not in sptSet, there is an
6263
// edge from u to v, and total weight of path from src to
@@ -118,7 +119,7 @@ int[] shortestPath(Graph graph, int src) {
118119
// Relax all edges |V| - 1 times.
119120
// A simple shortest path from src to any other vertex
120121
// can have at-most |V| - 1 edges.
121-
ArrayList<Edge> edges = graph.getEdgesList();
122+
List<Edge> edges = graph.getEdgesList();
122123
for (int i = 1; i < V; i++) {
123124
for (int j = 0; j < E; j++) {
124125
int u = edges.get(j).src.val;

src/com/michaelho/DynamicProgramming/DPTests.java

+2-35
Original file line numberDiff line numberDiff line change
@@ -121,42 +121,9 @@ public void testBalancedBST() {
121121
assertTrue(dp2.isBalancedBinaryTree(new TreeNode(treeArr)));
122122
}
123123

124-
private Graph createGraph(boolean negative) {
125-
int V = 5; // Number of vertices in graph
126-
int E = 8; // Number of edges in graph
127-
128-
Graph graph = new Graph(V, E);
129-
130-
// add edge 0-1
131-
graph.addEdge(0, 1, negative ? -1 : 1);
132-
133-
// add edge 0-2
134-
graph.addEdge(0, 2, 4);
135-
136-
// add edge 1-2
137-
graph.addEdge(1, 2, 3);
138-
139-
// add edge 1-3
140-
graph.addEdge(1, 3, 2);
141-
142-
// add edge 1-4
143-
graph.addEdge(1, 4, 2);
144-
145-
// add edge 3-2
146-
graph.addEdge(3, 2, 5);
147-
148-
// add edge 3-1
149-
graph.addEdge(3, 1, 1);
150-
151-
// add edge 4-3
152-
graph.addEdge(4, 3, negative ? -3 : 3);
153-
154-
return graph;
155-
}
156-
157124
@Test
158125
public void testBellmanFord() {
159-
Graph graph = createGraph(true);
126+
Graph graph = Graph.createGraph(true);
160127
int[] dist = dp3.bf.shortestPath(graph, 0);
161128
int[] result = {0, -1, 2, -2, 1};
162129
for (int i = 0; i < dist.length; i++) {
@@ -166,7 +133,7 @@ public void testBellmanFord() {
166133

167134
@Test
168135
public void testDijkstra() {
169-
Graph graph = createGraph(false);
136+
Graph graph = Graph.createGraph(false);
170137
int[] dist = dp3.dij.shortestPath(graph, 0);
171138
int[] result = {0, 1, 4, 3, 3};
172139
for (int i = 0; i < dist.length; i++) {

0 commit comments

Comments
 (0)