Skip to content

Commit 9e4b72b

Browse files
committed
Chapter 19 method stubs and descriptions + new data structure
1 parent ddbdff7 commit 9e4b72b

File tree

13 files changed

+229
-0
lines changed

13 files changed

+229
-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 label;
7+
public List<GraphVertex> edges;
8+
public boolean visited;
9+
10+
public GraphVertex(int label) {
11+
this.label = label;
12+
this.edges = new ArrayList<>();
13+
this.visited = false;
14+
}
15+
16+
public GraphVertex(int label, GraphVertex... graphVertices) {
17+
this.label = label;
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 (label != that.label) return false;
30+
return edges != null ? edges.equals(that.edges) : that.edges == null;
31+
}
32+
}

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 label 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<Character>> board) {
14+
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class ComputeShortestPath {
2+
3+
/*
4+
19.9
5+
6+
Design an algorithm which takes as input a graph G = (V,E),
7+
directed or undirected, a non-negative cost function on E,
8+
and vertices s and t; your algorithm should output a path
9+
with the fewest edges amongst all shortest paths from s to t.
10+
*/
11+
12+
public static void compute(GraphVertex s, GraphVertex t) {
13+
14+
}
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 class ColoredGraphVertex {
13+
public static enum Color {WHITE, GRAY, BLACK}
14+
15+
public Color color;
16+
public List<ColoredGraphVertex> edges;
17+
}
18+
19+
public static boolean isDeadlocked(List<ColoredGraphVertex> G) {
20+
21+
return false;
22+
}
23+
}
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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Collections;
2+
import java.util.List;
3+
4+
public class SearchMaze {
5+
6+
/*
7+
19.1
8+
9+
Given a 2D array of black and white entries representing
10+
a maze with designated entrance and exit points, find a
11+
path from the entrance to teh exit, if one exists.
12+
*/
13+
14+
public static enum Color {WHITE, BLACK}
15+
16+
public static List<Tuple> searchMaze(List<List<Color>> maze, Tuple s, Tuple e) {
17+
18+
return Collections.emptyList();
19+
}
20+
}

0 commit comments

Comments
 (0)