Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Graphs
Puzzle
Four people need to cross a rickety footbridge; they all begin on the same
side. It is dark, and they have one flashlight. A maximum of two people
can cross the bridge at one time. Any party that crosses, either one or two
people, must have the flashlight with them. The flashlight must be walked
back and forth; it cannot be thrown, for example. Person 1 takes 1 minute
to cross the bridge, person 2 takes 2 minutes, person 3 takes 5 minutes,
and person 4 takes 10 minutes. A pair must walk together at the rate of the
slower person’s pace. For example, if person 1 and person 4 walk together,
it will take them 10 minutes to get to the other side. If person 4 returns the
flashlight, a total of 20 minutes have passed. Can they cross the bridge in
17 minutes?
24 - 2
Puzzle
• Some positive integers are arranged in a triangle like the
one shown below. Design an algorithm (more efficient than
an exhaustive search, of course) to find the largest sum in a
descent from its apex to the base through a sequence of
adjacent numbers, one number per each level.
24 - 3
Chapter Scope
• Directed and undirected graphs
• Weighted graphs (networks)
• Common graph algorithms
W Y
Z
24 - 5
24 - 6
A Model
• A vertex : an island
• An edge : a path(bridge) between two islands
X
X
e1 e6
e2
W Y
W Y e5
e4
e3 e7
Z
Z
24 - 7
Graphs
• Like trees, graphs are made up of nodes and the
connections between those nodes.
• In graph terminology, we refer to the nodes as
vertices and refer to the connections among
them as edges.
• Vertices are typically referenced by a name or
label.
• Edges are referenced by a pairing of the vertices
(A, B) that they connect.
• An undirected graph is a graph where the
pairings representing the edges are unordered.
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 8
Graphs
• A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
• An edge e = (u,v) is a pair of vertices
• Example:
V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e),
(d,e)}
24 - 9
Graph
• A graph G is an ordered pair (V, E) consisting
of:
– A vertex set V = {W, X, Y, Z}
– An edge set E = {e1, e2, e3, e4, e5, e6, e7}
X
e1 e6
e2
W
e5 Y
e4
e3 e7
Z
The size of a graph is the number of elements in its vertex
set. 24 - 10
Graph Applications
• Electronic circuits
• Networks (roads, flights, communications)
• Visibility problems
• etc. CS16
JFK
LAX
LA STL
HNL
DFW
FTL
24 - 11
Undirected Graphs
• An undirected graph:
C D
24 - 14
Undirected Graphs
• Loop : An edge whose endpoints are equal.
• Multiple edges : Edges have the same pair of
endpoints.
Multiple
edges
Self-loop
24 - 15
Undirected Graphs
• Complete graphs
24 - 16
Undirected Graphs
• Simple graph : A graph has no loops or
multiple edges.
Multiple
edges loop
24 - 17
Undirected Graphs
• A path is a sequence of edges that connects two
vertices in a graph.
• A path is simple if each vertex is distinct.
• A circuit is a path in which the terminal vertex
coincides with the initial vertex.
• The length of a path is the number of edges in
the path (or the number of vertices minus 1).
• An undirected graph is considered connected if
for any two vertices in the graph there is a path
between them.
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 18
Undirected Graphs
• Simple path, path, and circuit
1 2 3
4 5 6
Simple path: [ 1, 2, 4, 5 ]
Path: [ 1, 2, 4, 5, 4]
Circuit: [ 1, 2, 4, 5, 4, 1]
24 - 19
Undirected Graphs
• An example of an undirected graph that is not
connected:
How to check
if a graph is
connected?
24 - 20
Undirected Graphs
• H1 and H2 are connected.
• H3 is disconnected.
a b a b
c H3 c
H1 H2 e
d d
e d
24 - 21
Cycles
• A cycle is a path in which the first and last
vertices are the same and none of the edges are
repeated.
• A graph that has no cycles is called acyclic.
Cycle
24 - 22
Directed Graphs
• A directed graph, sometimes referred to as a
digraph, is a graph where the edges are ordered
pairs of vertices.
• This means that the edges (A, B) and (B, A) are
separate, directional edges in a directed graph.
• If number of vertices = n, then there are n(n-1)
edges.
24 - 23
Directed Graphs
• A directed graph with
– vertices A, B, C, D
– edges (A, B), (A, D), (B, C), (B, D) and (D, C)
Why?
A B
C D E F
G H
Q Expanded
1
2
3
4
5 35
BFS: Example
S
A B
C D E F
G H
Q Expanded
1 S S
2
3
4
5 36
BFS: Example
S
A B
C D E F
G H
Q Expanded
1 S S
2 A,B S,A,B
3
4
5 37
BFS: Example
S
A B
C D E F
G H
Q Expanded
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4
5 38
BFS: Example
S
A B
C D E F
G H
Q Expanded
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 39
BFS: Example
S
A B
C D E F
G H
Q Expanded
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 40
BFS: Example
S
A B
C D E F
G H
Q Expanded
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 41
BFS: Example
S
A B
C D E F
G H
Q Expanded
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 42
BFS: Example
S
A B
C D E F
G H
Q Expanded
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9 43
BFS: Example
S
A B
C D E F
G H
Q Expanded
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9 H S,A,B,C,D,E,F,G,H
10 44
BFS: Example
S
A B
C D E F
G H
Q Expanded
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9 S,A,B,C,D,E,F,G,H
10 S,A,B,C,D,E,F,G,H 45
BFS: Example
Q Expanded
S
1 S A
2 A,B S,A,B A B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F C D E F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
G H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9 H S,A,B,C,D,E,F,G,H
10 S,A,B,C,D,E,F,G,H
46
Tracing the BFS Algorithm
• Trace of the BFS algorithm on the following graph
from A.
A B
C D E F
G H
S Expanded
1
2
3
4
5
51
6
DFS: Example
S
A B
C D E F
G H
S Expanded
1 S S
2
3
4
5
52
6
DFS: Example
S
A B
C D E F
G H
S Expanded
1 S S
2 A,B S,A,B
3
4
5
53
6
DFS: Example
S
A B
C D E F
G H
S Expanded
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4
5
54
6
DFS: Example
S
A B
C D E F
G H
S Expanded
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
55
6
DFS: Example
S
A B
C D E F
G H
S Expanded
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
56
6 D,B S,A,B,C,D,G,H
DFS: Example
S
A B
C D E F
G H
S Expanded
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
57
6 D,B S,A,B,C,D,G,H
Tracing the DFS Algorithm
• Trace of the DFS algorithm on the following graph
from A.
BFS DFS
or or or
68
Minimum Spanning Tree
• A minimum spanning tree (MST) is a spanning
tree where the sum of the weights of the edges
is less than or equal to the sum of the weights for
any other spanning tree for the same graph.
2 2
5 3 3
1 1
7
a b a
2
5 3
4
c 1 d
7
a b a
2
5 3
4
c 1 d d
7
a b a
2
5 3
4
c 1 d c d
7
a b a b
2
5 3
4
c 1 d c d
queue.addElement(0, startVertex);
while(!queue.isEmpty()),
vertex = queue.removeMin();
if (vertex.getContent().compareTo(targetContent) == 0) {
goalVertex = vertex;
break;
}
for non-visited neighbor of vertex,
if (vertex.getGvalue() + cost(vertex, neighbor) <
neighbor.getGvalue()) {
mark neighbor ‘visited from’ vertex;
neighbor.setGvalue(vertex.getGvalue() +
cost(vertex, neighbor));
queue.update(neighbor.getGvalue(), neighbor);
}