Skip to content

Commit a9b4f2f

Browse files
committed
Merge branch 'develop'
Chapter 19: Graphs
2 parents 479c0de + 53081da commit a9b4f2f

23 files changed

+946
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
public class GraphVertex {
6+
public int data;
7+
public List<GraphVertex> edges;
8+
public boolean visited;
9+
10+
public GraphVertex(int data) {
11+
this.data = data;
12+
this.edges = new ArrayList<>();
13+
this.visited = false;
14+
}
15+
16+
public GraphVertex(int data, GraphVertex... graphVertices) {
17+
this.data = data;
18+
this.edges = Arrays.asList(graphVertices);
19+
this.visited = false;
20+
}
21+
22+
@Override
23+
public boolean equals(Object o) {
24+
if (this == o) return true;
25+
if (o == null || getClass() != o.getClass()) return false;
26+
27+
GraphVertex that = (GraphVertex) o;
28+
29+
if (data != that.data) return false;
30+
return edges != null ? edges.equals(that.edges) : that.edges == null;
31+
}
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
public class Vertex {
5+
public Map<Vertex, Integer> edges = new HashMap<>();
6+
public Character id;
7+
public boolean visited = false;
8+
9+
public Vertex(Character id) {
10+
this.id = id;
11+
}
12+
13+
@Override
14+
public boolean equals(Object o) {
15+
if (this == o) return true;
16+
if (o == null || getClass() != o.getClass()) return false;
17+
18+
Vertex vertex = (Vertex) o;
19+
20+
if (visited != vertex.visited) return false;
21+
if (edges != null ? !edges.equals(vertex.edges) : vertex.edges != null) return false;
22+
return id != null ? id.equals(vertex.id) : vertex.id == null;
23+
}
24+
}

graphs/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Chapter 19: Graphs
2+
3+
* 19.1 SearchMaze
4+
* 19.2 PaintBooleanMatrix
5+
* 19.3 ComputeEnclosedRegions
6+
* 19.4 DeadlockDetection
7+
* 19.5 CloneAGraph
8+
* 19.6 MakingWiredConnections
9+
* 19.7 TransformOneStringToAnother
10+
* 19.8 TeamPhotoDay
11+
* 19.9 ComputeShortestPath

graphs/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>elements-of-programming-interviews</artifactId>
7+
<groupId>gardncl</groupId>
8+
<version>1.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>graphs</artifactId>
13+
<name>Chapter 19: Graphs</name>
14+
<dependencies>
15+
<dependency>
16+
<groupId>gardncl</groupId>
17+
<artifactId>datastructures</artifactId>
18+
<version>1.0</version>
19+
</dependency>
20+
</dependencies>
21+
22+
23+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class CloneAGraph {
2+
3+
/*
4+
19.5
5+
6+
Consider a vertex type for a directed graph in which there
7+
are two fields: an integer data and a list of references
8+
to other vertices. Design an algorithm that takes a reference
9+
to a vertex u, and creates a copy of the graph on the vertices
10+
reachable from u. Return the copy of u.
11+
*/
12+
13+
public static GraphVertex cloneGraph(GraphVertex g) {
14+
15+
return new GraphVertex(0);
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.List;
2+
3+
public class ComputeEnclosedRegions {
4+
5+
/*
6+
19.3
7+
8+
Let A by a 2D array whose entries are either W or B.
9+
Write a program that takes A, and replaces all Ws
10+
that cannot reach the boundary with a B.
11+
*/
12+
13+
public static void fillSurroundingRegions(List<List<Boolean>> board) {
14+
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.Collections;
2+
import java.util.List;
3+
4+
public class ComputeShortestPath {
5+
6+
/*
7+
19.9
8+
9+
Design an algorithm which takes as input a graph G = (V,E),
10+
directed or undirected, a non-negative cost function on E,
11+
and vertices s and t; your algorithm should output a path
12+
with the fewest edges amongst all shortest paths from s to t.
13+
*/
14+
15+
public static List<Vertex> compute(int cost, List<Vertex> graph, Vertex s, Vertex t) {
16+
17+
return Collections.emptyList();
18+
}
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.List;
2+
3+
public class DeadlockDetection {
4+
5+
/*
6+
19.4
7+
8+
Write a program that takes as input a directed graph
9+
and checks if the graph contains a cycle.
10+
*/
11+
12+
public static boolean isDeadlocked(List<GraphVertex> G) {
13+
14+
return false;
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.List;
2+
3+
public class MakingWiredConnections {
4+
5+
/*
6+
19.6
7+
8+
Design an algorithm that takes a set of pins and a set of wires
9+
connecting pairs of pins, and determines if it is possible to
10+
place some pins on the left half of a PCB, and the remainder on
11+
the right half, such that each wire is between left and right
12+
halves.
13+
*/
14+
15+
public static boolean isAnyPlacementFeasible(List<GraphVertex> G) {
16+
17+
return false;
18+
}
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.List;
2+
3+
public class PaintBooleanMatrix {
4+
5+
/*
6+
19.2
7+
8+
Implement a routine that takes an n x m Boolean array A
9+
together with an entry (x,y) and flips the color of the
10+
region associated with (x,y).
11+
*/
12+
13+
public static void flipColor(List<List<Boolean>> A, int x, int y) {
14+
15+
}
16+
}

0 commit comments

Comments
 (0)