Professional Documents
Culture Documents
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
1 rights reserved. 0136012671
2
2097 Boston C
983 D
Chicago Island 1
1331 214 Island 2
807
1003 New York
787
Denver
533 B
1267 1260
599
888
San Francisco 1015 Kansas City A
381 1663
864
496
Los Angeles 1435 Atlanta C D
781
810
Dallas
661
239
B
Houston 1187
Miami
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
3 rights reserved. 0136012671
4
Basic Graph Terminologies Basic Graph Terminologies
What is a graph? Parallel edge
Define a graph Simple graph
Directed vs. undirected graphs Complete graph
Weighted vs. unweighted graphs Spanning tree
Adjacent vertices
Incident
Degree
Neighbor
loop
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
5 rights reserved. 0136012671
6
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
7 rights reserved. 0136012671
8
interface
Graph
+getSize(): int
+getVertices(): Object[]
+getVertex(index: int): Object
+getIndex(v: Object): int
Returns the number of vertices in the graph.
Returns the vertices in the graph.
Returns the vertex object for the specified vertex label.
Returns the index for the specified vertex object.
Graph Traversals
+getNeighbors(v: int): java.util.LinkedList
+getDegree(v: int): int
Returns the neighbors of vertex v.
Returns the degree for a specified vertex. Depth-first search and breadth-first search
+getAdjacencyMatrix(): int[][]
+printAdjacencyMatrix(): void
Returns the adjacency matrix.
Prints the adjacency matrix.
Graph
+printEdges(): void Prints the edges.
Obtains a depth-first search tree.
+dfs(int v): AbstractGraph.Tree
+bfs(int v): AbstractGraph.Tree Obtain a breadth-first search tree. Both traversals result in a spanning tree, which can be
AbstractGraph
AbstractGraph modeled using a class.
#vertices: Object[] Vertices in the graph. AbstractGraph.Tree
#neighbors: java.util.LinkedList[] Neighbors for each vertex in the graph.
#AbstractGraph(edges: int[][], vertices: Constructs a graph with specified edges and vertices in -root: int The root of the tree.
Object[])
#AbstractGraph(edges: List<Edge>,
arrays.
Constructs a graph with the specified edges and vertices UnweightedGraph -parent: int[] The parents of the vertices.
vertices: List) stored in lists. -searchOrders: java.util.List<Integer> The orders for traversing the vertices.
#AbstractGraph(edges: int[][], Constructs a graph with specified edges in an array and
numberOfVertices: int) the number of vertices. +Tree(root: int, parent: int[], Constructs a tree with the specified root, parent, and
#AbstractGraph(edges: List<Edge>, Constructs a graph with specified edges in a list and the searchOrders: List<Integer>) searchOrders.
numberOfVertices: int) number of vertices.
Inner classes Tree and PathIterator are
defined here
TestGraph +Tree(root: int, parent: int[]) Constructs a tree with the specified root and parent with
no specified searchOrders.
+getRoot(): int Returns the root of the tree.
UnweightedGraph +getSearchOrders(): List Returns the order of vertices searched.
+UnweightedGraph(edges: int[][], vertices: Constructs a graph with the specified edges and vertices +getParent(v: int): int Returns the parent of vertex v.
Object[]) in arrays.
+getNumberOfVerticesFound(): int Returns the number of vertices searched.
+UnweightedGraph(edges: List<Edge>, Constructs a graph with the specified edges and vertices
vertices: List) stored in lists. +pathIterator(v: int): java.util.Iterator Returns an iterator for the path from the root to vertex v.
+UnweightedGraph(edges: List<Edge>, Constructs a graph with specified edges in a list and the
numberOfVertices: int)
+printPath(v: int): void Displays a path from the root to the specified vertex.
number of vertices.
+UnweightedGraph(edges: int[][], Constructs a graph with specified edges in an array and +printTree(): void Displays tree with the root and all edges.
numberOfVertices: int) the number of vertices.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
9 rights reserved. 0136012671
10
dfs(vertex v) { 3 4 3 4 3 4
visit v;
for each neighbor w of v 0 0
1 1
}
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
11 rights reserved. 0136012671
12
Depth-First Search Example Applications of the DFS
Detecting whether a graph is connected. A graph is connected if there
Seattle
is a path between any two vertices in the graph. (See Exercise 27.1)
2097 Boston
983
Chicago
807
1331 214 Detecting whether there is a path between two vertices. (See Exercise
1003 New York
Denver
787
27.2)
533
1267 1260
599
888
San Francisco 1015 Kansas City Finding a path between two vertices. (See Exercise 27.3)
381 1663
864
496
Los Angeles 1435
781
Atlanta Finding all connected components. A connected component is a
Dallas
810
661
maximal connected subgraph in which every pair of vertices are
239
connected by a path. (See Exercise 27.4)
Houston 1187
Miami
0 1 0 1 0 1 2097 Boston
983
Chicago
1331 214
807
2 2 2 1003 New York
787
Denver
533
1267 1260
599
3 4 3 4 3 4 888
San Francisco 1015 Kansas City
381 1663
864
496
Los Angeles 1435 Atlanta
781
810
Dallas
661
239
Houston 1187
Miami
TestBFS TestBFS
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
17 rights reserved. 0136012671
18
0
1
3
4
7
KnightTourModel KnightTourApp KnightTourApp
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All
rights reserved. 0136012671
23 rights reserved. 0136012671
24