You are on page 1of 6

Objectives

) To give applications of graphs and explain the Seven Bridges of


Knigsberg problem (27.1).
) To describe the graph terminologies: vertices, edges, simple graphs,
weighted/unweighted graphs, and directed/undirected graphs (27.2).
Chapter 27 Graph Applications ) To represent vertices and edges using lists, adjacent matrices, and
adjacent lists (27.3).
) To model graphs using the Graph interface, the AbstractGraph class,
and the UnweightedGraph class (27.4).
) To represent the traversal of a graph using the AbstractGraph.Tree
class (27.5).
) To design and implement depth-first search (27.6).
) To design and implement breadth-first search (27.7).
) To solve the nine tail problem using breadth-first search (27.8).

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

Modeling Using Graphs Seven Bridges of Knigsberg


A
Seattle

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

Representing Graphs Modeling Graphs


Representing Vertices
Representing Edges: Edge Array UnweightedGraph

Representing Edges: Edge Objects Graph AbstractGraph


WeightedGraph
Representing Edges: Adjacency Matrices
Representing Edges: Adjacency Lists
Interface Abstract Class Concrete Classes

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

Depth-First Search Depth-First Search Example


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 0 1 0 1 0 1
search starts from the root. In a graph, the search can start from any
vertex. 2 2 2

dfs(vertex v) { 3 4 3 4 3 4

visit v;
for each neighbor w of v 0 0
1 1

if (w has not been visited) {


2 2
dfs(w);
} 3 4 3 4

}
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

Detecting whether there is a cycle in the graph. (See Exercise 27.5)


TestDFS TestDFS
Finding a cycle in the graph. (See Exercise 27.5)
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
13 rights reserved. 0136012671
14

Breadth-First Search Breadth-First Search Algorithm


The breadth-first traversal of a graph is like the breadth- bfs(vertex v) {
create an empty queue for storing vertices to be visited;
first traversal of a tree discussed in 25.2.3, Tree
add v into the queue;
Traversal. With breadth-first traversal of a tree, the mark v visited;
nodes are visited level by level. First the root is visited, while the queue is not empty {
then all the children of the root, then the grandchildren of dequeue a vertex, say u, from the queue
the root from left to right, and so on. visit u;
for each neighbor w of u
if w has not been visited {
add w into the queue;
mark w visited;
}
}
}
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
15 rights reserved. 0136012671
16
Breadth-First Search Example Breadth-First Search Example
Seattle

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

Applications of the BFS The Nine Tail Problem


Detecting whether a graph is connected. A graph is connected if there is a path
between any two vertices in the graph. The problem is stated as follows. Nine coins are placed in a
Detecting whether there is a path between two vertices.
three by three matrix with some face up and some face
down. A legal move is to take any coin that is face up and
Finding a shortest path between two vertices. You can prove that the path between reverse it, together with the coins adjacent to it (this does
the root and any node in the BFS tree is the shortest path between the root and the not include coins that are diagonally adjacent). Your task is
node (see Review Question 27.10).
to find the minimum number of the moves that lead to all
Finding all connected components. A connected component is a maximal connected coins face down.
subgraph in which every pair of vertices are connected by a path.

Detecting whether there is a cycle in the graph. (See Exercise 27.4) H H H H H H T T T


Finding a cycle in the graph. (See Exercise 27.5)
T T T T H T T T T
Testing whether a graph is bipartite. A graph is bipartite if the vertices of the graph T T T
can be divided into two disjoint sets such that no edges exist between vertices in the
H H H T T T
same set. (See Exercise 27.8)
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
19 rights reserved. 0136012671
20
The Nine Tail Problem The Hamiltonian Path Problem
A Hamiltonian path in a graph is a path that visits
each vertex in the graph exactly once. A
Hamiltonian cycle is a cycle that visits each vertex
in the graph exactly once and returns to the starting
vertex.
A Hamiltonian path and cycle have many
applications.

NineTailModel NineTailApp NineTailApp


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
21 rights reserved. 0136012671
22

The Knights Tour Problem Find a Knights Tour


The Knights Tour is a well-known classic problem. The To solve the Knights Tour problem, create a graph with
objective is to move a knight, starting from any square on 64 vertices representing all the squares in the chessboard.
a chessboard, to every other square once. Note that the Two vertices are connected if a knight can move between
knight makes only L-shape moves (two spaces in one the two vertices.
direction and one space in a perpendicular direction). As
shown in Figure 27.19(a), the knight can move to eight
squares.
0 1 2 3 4 5 6 7

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

You might also like