Chapter 10 Graph
Chapter 10 Graph
1
Modeling Using Graphs
2
Seven Bridges of Königsberg
A
C
D
Island 1
Island 2
C D
3
Basic Graph Terminologies
Peter Jane
Cindy Mark
Wendy
5
Basic Graph Terminologies
Parallel edge
Simple graph
Complete graph
Spanning tree
A D
A D
B E
B E
C
C
6
Representing Graphs
Representing Vertices
Representing Edges: Edge Array
Representing Edges: Edge Objects
Representing Edges: Adjacency Matrices
Representing Edges: Adjacency Lists
7
Representing Vertices
String[] vertices = {“Seattle“, “San Francisco“, “Los Angles”,
“Denver”, “Kansas City”, “Chicago”, … };
City[] vertices = {city0, city1, … };
List<String> vertices;
8
Representing Edges: Edge Array
int[][] edges = {{0, 1}, {0, 3} {0, 5}, {1, 0}, {1, 2}, … };
9
Representing Edges: Edge Object
public class Edge {
int u, v;
public Edge(int u, int v) {
this.u = u;
this.v = v;
}
10
Representing Edges: Adjacency Matrix
int[][] adjacencyMatrix = {
{0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0}, // Seattle
{1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, // San Francisco
{0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0}, // Los Angeles
{1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0}, // Denver
{0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0}, // Kansas City
{1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0}, // Chicago
{0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0}, // Boston
{0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0}, // New York
{0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1}, // Atlanta
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1}, // Miami
{0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1}, // Dallas
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0} // Houston
};
11
Representing Edges: Adjacency Vertex List
List<Integer>[] neighbors = new List[12];
Seattle neighbors[0] 1 3 5
San Francisco neighbors[1] 0 2 3
Los Angeles neighbors[2] 1 3 4 10
Denver neighbors[3] 0 1 2 4 5
Kansas City neighbors[4] 2 3 5 7 8 10
Chicago neighbors[5] 0 3 4 6 7
Boston neighbors[6] 5 7
New York neighbors[7] 4 5 6 8
Atlanta neighbors[8] 4 7 9 10 11
Miami neighbors[9] 8 11
Dallas neighbors[10] 2 4 8 11
Houston neighbors[11] 8 9 10
13
Representing Adjacency Edge List Using ArrayList
List<ArrayList<Edge>> neighbors = new ArrayList<>();
neighbors.add(new ArrayList<Edge>());
neighbors.get(0).add(new Edge(0, 1));
neighbors.get(0).add(new Edge(0, 3));
neighbors.get(0).add(new Edge(0, 5));
neighbors.add(new ArrayList<Edge>());
neighbors.get(1).add(new Edge(1, 0));
neighbors.get(1).add(new Edge(1, 2));
neighbors.get(1).add(new Edge(1, 3));
...
...
neighbors.get(11).add(new Edge(11, 8));
neighbors.get(11).add(new Edge(11, 9));
neighbors.get(11).add(new Edge(11, 10));
14
Modeling Graphs
15
«interface» The generic type V is the type for vertices.
Graph<V>
+getSize(): int Returns the number of vertices in the graph.
+getVertices(): List<V> Returns the vertices in the graph.
+getVertex(index: int): V Returns the vertex object for the specified vertex index.
+getIndex(v: V): int Returns the index for the specified vertex.
+getNeighbors(index: int): List<Integer> Returns the neighbors of vertex with the specified index.
+getDegree(index: int): int Returns the degree for a specified vertex index.
+printEdges(): void
Prints the edges.
+clear(): void Clears the graph.
+addVertex(v: V): boolean Returns true if v is added to the graph. Returns false if v
is already in the graph.
+addEdge(u: int, v: int): boolean Adds an edge from u to v to the graph throws
IllegalArgumentException if u or v is invalid. Returns
true if the edge is added and false if (u, v) is already in
the graph.
Adds an edge into the adjacency edge list.
+addEdge(e: Edge): boolean
+remove(v: V): boolean Removes a vertex from the graph.
+remove(u: int, v: int): boolean Removes an edge from the graph.
+dfs(v: int): UnWeightedGraph<V>.SearchTree Obtains a depth-first search tree starting from v.
+bfs(v: int): UnWeightedGraph<V>.SearchTree Obtains a breadth-first search tree starting from v.
.
UnweightedGraph<V>
#vertices: List<V> Vertices in the graph.
#neighbors: List<List<Edge>> Neighbors for each vertex in the graph.
+UnweightedGraph() Constructs an empty graph.
+UnweightedGraph(vertices: V[], edges: Constructs a graph with the specified edges and vertices
int[][]) stored in arrays.
+UnweightedGraph(vertices: List<V>, Constructs a graph with the specified edges and vertices
edges: List<Edge>)
stored in lists.
+UnweightedGraph(edges: int[][], Constructs a graph with the specified edges in an array
numberOfVertices: int) and the integer vertices 1, 2, ....
+UnweightedGraph(edges: List<Edge>, Constructs a graph with the specified edges in a list and
numberOfVertices: int) the integer vertices 1, 2, ….
16
Graph Traversals
Depth-first search and breadth-first search
+SearchTree(root: int, parent: Constructs a tree with the specified root, parent, and
int[], searchOrder: List<Integer>) searchOrder.
+getRoot(): int Returns the root of the tree.
+getSearchOrder(): List<Integer> Returns the order of vertices searched.
+getParent(index: int): int Returns the parent for the specified vertex index.
+getNumberOfVerticesFound(): int Returns the number of vertices searched.
+getPath(index: int): List<V> Returns a list of vertices from the specified vertex index
to the root.
+printPath(index: int): void Displays a path from the root to the specified vertex.
+printTree(): void
Displays tree with the root and all edges.
17
Depth-First Search
The depth-first search of a graph is like the depth-first search of a
tree discussed in §25.2.3, “Tree Traversal.” In the case of a tree, the
search starts from the root. In a graph, the search can start from any
vertex.
19
Depth-First Search Example
Seattle
2097 Boston
983
Chicago
1331 214
807
1003 New York
787
Denver
533
1267 1260
599
888
San Francisco 1015 Kansas City
381 1663
864
496
Los Angeles 1435 Atlanta
781
810
Dallas
661
239
Houston 1187
Miami
20
public static void main(String[] args) {
String[] vertices = {"Seattle", "San Francisco", "Los Angeles",
"Denver", "Kansas City", "Chicago", "Boston", "New York",
"Atlanta", "Miami", "Dallas", "Houston"};
int[][] edges = {
{0, 1}, {0, 3}, {0, 5},
{1, 0}, {1, 2}, {1, 3},
{2, 1}, {2, 3}, {2, 4}, {2, 10},
{3, 0}, {3, 1}, {3, 2}, {3, 4}, {3, 5},
{4, 2}, {4, 3}, {4, 5}, {4, 7}, {4, 8}, {4, 10},
{5, 0}, {5, 3}, {5, 4}, {5, 6}, {5, 7},
{6, 5}, {6, 7},
{7, 4}, {7, 5}, {7, 6}, {7, 8},
{8, 4}, {8, 7}, {8, 9}, {8, 10}, {8, 11},
{9, 8}, {9, 11},
{10, 2}, {10, 4}, {10, 8}, {10, 11},
{11, 8}, {11, 9}, {11, 10}
};
21
Graph<String> graph = new UnweightedGraph<>(vertices, edges);
UnweightedGraph<String>.SearchTree dfs =
graph.dfs(graph.getIndex("Chicago")); // Get a dfs starting at Chicago
22
Applications of the DFS
Detecting whether a graph is connected. Search the graph starting from
any vertex. If the number of vertices searched is the same as the number
of vertices in the graph, the graph is connected. Otherwise, the graph is
not connected.
24
Breadth-First Search Algorithm
Input: G = (V, E) and a starting vertex v
Output: a BFS tree rooted at v
bfs(vertex v) {
create an empty queue for storing vertices to be visited;
add v into the queue;
mark v visited;
while the queue is not empty {
dequeue a vertex, say u, from the queue
process u;
for each neighbor w of u
if w has not been visited {
add w into the queue;
set u as the parent for w;
mark w visited;
}
}
25
}
Breadth-First Search Example
26
Breadth-First Search Example
Seattle
2097 Boston
983
Chicago
1331 214
807
1003 New York
787
Denver
533
1267 1260
599
888
San Francisco 1015 Kansas City
381 1663
864
496
Los Angeles 1435 Atlanta
781
810
Dallas
661
239
Houston 1187
Miami
27
public class TestBFS {
public static void main(String[] args) {
String[] vertices = {"Seattle", "San Francisco", "Los Angeles",
"Denver", "Kansas City", "Chicago", "Boston", "New York",
"Atlanta", "Miami", "Dallas", "Houston"};
int[][] edges = {
{0, 1}, {0, 3}, {0, 5},
{1, 0}, {1, 2}, {1, 3},
{2, 1}, {2, 3}, {2, 4}, {2, 10},
{3, 0}, {3, 1}, {3, 2}, {3, 4}, {3, 5},
{4, 2}, {4, 3}, {4, 5}, {4, 7}, {4, 8}, {4, 10},
{5, 0}, {5, 3}, {5, 4}, {5, 6}, {5, 7},
{6, 5}, {6, 7},
{7, 4}, {7, 5}, {7, 6}, {7, 8},
{8, 4}, {8, 7}, {8, 9}, {8, 10}, {8, 11},
{9, 8}, {9, 11},
{10, 2}, {10, 4}, {10, 8}, {10, 11},
{11, 8}, {11, 9}, {11, 10}
};
28
Graph<String> graph = new UnweightedGraph<>(vertices, edges);
UnweightedGraph<String>.SearchTree bfs =
graph.bfs(graph.getIndex("Chicago")); // Get a dfs starting at Chicago
29