-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 19: Graphs
- Loading branch information
Showing
23 changed files
with
946 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class GraphVertex { | ||
public int data; | ||
public List<GraphVertex> edges; | ||
public boolean visited; | ||
|
||
public GraphVertex(int data) { | ||
this.data = data; | ||
this.edges = new ArrayList<>(); | ||
this.visited = false; | ||
} | ||
|
||
public GraphVertex(int data, GraphVertex... graphVertices) { | ||
this.data = data; | ||
this.edges = Arrays.asList(graphVertices); | ||
this.visited = false; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
GraphVertex that = (GraphVertex) o; | ||
|
||
if (data != that.data) return false; | ||
return edges != null ? edges.equals(that.edges) : that.edges == null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Vertex { | ||
public Map<Vertex, Integer> edges = new HashMap<>(); | ||
public Character id; | ||
public boolean visited = false; | ||
|
||
public Vertex(Character id) { | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Vertex vertex = (Vertex) o; | ||
|
||
if (visited != vertex.visited) return false; | ||
if (edges != null ? !edges.equals(vertex.edges) : vertex.edges != null) return false; | ||
return id != null ? id.equals(vertex.id) : vertex.id == null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Chapter 19: Graphs | ||
|
||
* 19.1 SearchMaze | ||
* 19.2 PaintBooleanMatrix | ||
* 19.3 ComputeEnclosedRegions | ||
* 19.4 DeadlockDetection | ||
* 19.5 CloneAGraph | ||
* 19.6 MakingWiredConnections | ||
* 19.7 TransformOneStringToAnother | ||
* 19.8 TeamPhotoDay | ||
* 19.9 ComputeShortestPath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>elements-of-programming-interviews</artifactId> | ||
<groupId>gardncl</groupId> | ||
<version>1.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>graphs</artifactId> | ||
<name>Chapter 19: Graphs</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>gardncl</groupId> | ||
<artifactId>datastructures</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
public class CloneAGraph { | ||
|
||
/* | ||
19.5 | ||
Consider a vertex type for a directed graph in which there | ||
are two fields: an integer data and a list of references | ||
to other vertices. Design an algorithm that takes a reference | ||
to a vertex u, and creates a copy of the graph on the vertices | ||
reachable from u. Return the copy of u. | ||
*/ | ||
|
||
public static GraphVertex cloneGraph(GraphVertex g) { | ||
|
||
return new GraphVertex(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import java.util.List; | ||
|
||
public class ComputeEnclosedRegions { | ||
|
||
/* | ||
19.3 | ||
Let A by a 2D array whose entries are either W or B. | ||
Write a program that takes A, and replaces all Ws | ||
that cannot reach the boundary with a B. | ||
*/ | ||
|
||
public static void fillSurroundingRegions(List<List<Boolean>> board) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ComputeShortestPath { | ||
|
||
/* | ||
19.9 | ||
Design an algorithm which takes as input a graph G = (V,E), | ||
directed or undirected, a non-negative cost function on E, | ||
and vertices s and t; your algorithm should output a path | ||
with the fewest edges amongst all shortest paths from s to t. | ||
*/ | ||
|
||
public static List<Vertex> compute(int cost, List<Vertex> graph, Vertex s, Vertex t) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import java.util.List; | ||
|
||
public class DeadlockDetection { | ||
|
||
/* | ||
19.4 | ||
Write a program that takes as input a directed graph | ||
and checks if the graph contains a cycle. | ||
*/ | ||
|
||
public static boolean isDeadlocked(List<GraphVertex> G) { | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import java.util.List; | ||
|
||
public class MakingWiredConnections { | ||
|
||
/* | ||
19.6 | ||
Design an algorithm that takes a set of pins and a set of wires | ||
connecting pairs of pins, and determines if it is possible to | ||
place some pins on the left half of a PCB, and the remainder on | ||
the right half, such that each wire is between left and right | ||
halves. | ||
*/ | ||
|
||
public static boolean isAnyPlacementFeasible(List<GraphVertex> G) { | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import java.util.List; | ||
|
||
public class PaintBooleanMatrix { | ||
|
||
/* | ||
19.2 | ||
Implement a routine that takes an n x m Boolean array A | ||
together with an entry (x,y) and flips the color of the | ||
region associated with (x,y). | ||
*/ | ||
|
||
public static void flipColor(List<List<Boolean>> A, int x, int y) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class SearchMaze { | ||
|
||
/* | ||
19.1 | ||
Given a 2D array of black and white entries representing | ||
a maze with designated entrance and exit points, find a | ||
path from the entrance to teh exit, if one exists. | ||
*/ | ||
|
||
public static List<Tuple> searchMaze(List<List<Boolean>> maze, Tuple s, Tuple e) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.List; | ||
|
||
public class TeamPhotoDay { | ||
|
||
/* | ||
19.8 | ||
How would you generalize your solution to Problem 14.8 on Page 252, | ||
to determine the largest number of teams that can be photographed | ||
simultaneously subject to the same constraints? | ||
*/ | ||
|
||
public static int findLargestNumberTeams(List<GraphVertex> G) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import java.util.Set; | ||
|
||
public class TransformOneStringToAnother { | ||
|
||
/* | ||
19.7 | ||
Given a dictionary D and two strings s and t, write | ||
a program to determine if s produces t. Assume that | ||
all characters are lowercase alphabets. If s does | ||
produce t, output the length of a shortest production | ||
sequence; otherwise, output -1. | ||
*/ | ||
|
||
public static int transformString(Set<String> D,String s, String d) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class CloneAGraphTest { | ||
|
||
private GraphVertex expected; | ||
private GraphVertex G; | ||
|
||
@Test | ||
public void cloneGraph1() throws Exception { | ||
GraphVertex a = new GraphVertex(0); | ||
GraphVertex b = new GraphVertex(1); | ||
GraphVertex c = new GraphVertex(2); | ||
GraphVertex d = new GraphVertex(3); | ||
a.edges.add(b); | ||
a.edges.add(c); | ||
b.edges.add(c); | ||
b.edges.add(d); | ||
|
||
expected = a; | ||
G = a; | ||
|
||
test(expected, G); | ||
} | ||
|
||
private void test(GraphVertex expected, GraphVertex G) { | ||
assertEquals(expected, CloneAGraph.cloneGraph(G)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ComputeEnclosedRegionsTest { | ||
|
||
private final Boolean w = true; | ||
private final Boolean b = false; | ||
private List<List<Boolean>> expected; | ||
private List<List<Boolean>> board; | ||
|
||
@Test | ||
public void fillSurroundingRegions1() throws Exception { | ||
expected = Arrays.asList( | ||
Arrays.asList(b,b,b,b), | ||
Arrays.asList(w,b,b,b), | ||
Arrays.asList(b,b,b,b), | ||
Arrays.asList(b,b,b,b) | ||
); | ||
board = Arrays.asList( | ||
Arrays.asList(b,b,b,b), | ||
Arrays.asList(w,b,w,b), | ||
Arrays.asList(b,w,w,b), | ||
Arrays.asList(b,b,b,b) | ||
); | ||
|
||
test(expected, board); | ||
} | ||
|
||
@Test | ||
public void fillSurroundingRegions2() throws Exception { | ||
expected = Arrays.asList( | ||
Arrays.asList(b,b,b,b,b,b,b,b,b,b), | ||
Arrays.asList(b,b,b,b,b,b,w,w,b,b), | ||
Arrays.asList(b,b,b,b,b,b,b,w,b,b), | ||
Arrays.asList(b,b,w,b,b,b,b,w,b,b), | ||
Arrays.asList(b,b,b,w,w,w,w,b,b,b), | ||
Arrays.asList(b,b,b,w,w,b,w,b,b,b), | ||
Arrays.asList(b,b,b,b,b,b,b,w,w,b), | ||
Arrays.asList(b,b,b,w,b,b,b,w,w,b), | ||
Arrays.asList(b,b,b,b,b,b,b,b,b,b), | ||
Arrays.asList(b,b,b,b,b,b,b,b,b,b) | ||
); | ||
board = Arrays.asList( | ||
Arrays.asList(b,w,b,w,w,w,b,b,b,b), | ||
Arrays.asList(w,w,b,w,w,b,w,w,b,b), | ||
Arrays.asList(b,b,b,w,w,b,b,w,b,b), | ||
Arrays.asList(w,b,w,b,b,b,b,w,b,w), | ||
Arrays.asList(b,w,b,w,w,w,w,b,w,w), | ||
Arrays.asList(b,w,b,w,w,b,w,b,b,b), | ||
Arrays.asList(w,w,w,b,b,w,b,w,w,b), | ||
Arrays.asList(b,w,b,w,b,w,b,w,w,b), | ||
Arrays.asList(b,w,b,b,w,w,w,b,b,b), | ||
Arrays.asList(w,w,w,w,w,w,w,b,b,w) | ||
); | ||
|
||
test(expected, board); | ||
} | ||
|
||
private void test(List<List<Boolean>> expected, List<List<Boolean>> board) { | ||
ComputeEnclosedRegions.fillSurroundingRegions(board); | ||
assertEquals(expected, board); | ||
} | ||
|
||
} |
Oops, something went wrong.