Sei sulla pagina 1di 111

Chapter 24

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

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 4


The KÖnigsberg Bridge Problem
• Königsberg is a city on the Pregel river in Prussia.
• The city occupied two islands plus areas on both
banks.
• Problem:
– Whether they could leave home, cross every bridge
exactly once, and return home.
X

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:

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 12


Undirected Graphs
• An edge in an undirected graph can be traversed
in either direction.
• Two vertices are said to be adjacent if there is an
edge connecting them.
• Adjacent vertices are sometimes referred to as
neighbors.
• An edge of a graph that connects a vertex to itself
is called a self-loop or sling.
• An undirected graph is considered complete if it
has the maximum number of edges connecting
vertices.
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 13
Undirected Graphs
• Two vertices are adjacent and are neighbors
if they are the endpoints of an edge.
• Example:
– A and B are adjacent.
– A and D are not adjacent.
A B

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

It is not simple. It is a simple graph.

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)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 24


Directed Graphs
• Previous definitions change slightly for directed
graphs.
– a path in a direct graph is a sequence of directed
edges that connects two vertices in a graph.
– a directed graph is connected if for any two vertices
in the graph there is a path between them
(considering directions).
– if a directed graph has no cycles, it is possible to
arrange the vertices such that vertex A precedes
vertex B if an edge exists from A to B.

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 25


Directed Graphs
• A connected directed graph and an unconnected
directed graph:

Why?

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 26


Weighted Graphs
• A weighted graph, sometimes called a network,
is a graph with weights (or costs) associated with
each edge.
• The weight of a path in a weighted graph is the
sum of the weights of the edges in the path.
• Weighted graphs may be either undirected or
directed.
• For weighted graphs, we represent each edge
with a triple including the starting vertex, ending
vertex, and the weight (Boston, New York, 120).
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 27
Weighted Graphs
• We could use an undirected network to
represent flights between cities.
• The weights are the cost.

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 28


Weighted Graphs
• A directed version of the graph could show
different costs depending on the direction.

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 29


Common Graph Algorithms
• There are a number of a common algorithms that
may apply to undirected, directed, and/or
weighted graphs.
• These include
– various traversal algorithms – searching
– algorithms for finding the shortest path
– algorithms for finding the least costly path in a network
– algorithms for answering simple questions (such as
connectivity)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 30


Graph Traversals
• There are two main types of graph traversal
algorithms.
– breadth-first (BFS): behaves much like a level-order traversal of
a tree
– depth-first (DFS): behaves much like the preorder traversal of a
tree

• One difference: there is no root node present in


a graph.
• Graph traversals may start at any vertex in the
graph.
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 31
Breadth-First Traversal (BFS)
• We can construct a breadth-first traversal for a
graph using a queue.
• We will use the queue to manage the traversal.
• Algorithm:
queue.???(the starting vertex);
while(!queue. ???())
vertex = queue. ???();
vertex. ???();
for each unvisited vertex, next, adjacent to vertex,
queue. ???(next);

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 32


Graph Traversals
• A traversal example:

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 33


Graph Traversals
• Breadth-first traversal starting from vertex 9:
1. Add 9 to the queue.
2. Dequeue 9 from the queue.
3. Mark 9 ‘visited’.
4. Add 6, 7, and 8 to the queue.
5. Dequeue 6 from the queue.
6. ...
The result?

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 34


BFS: Example
S

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.

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 47


Depth-First Traversal (DFS)
• Use nearly the same approach as the breadth-
first traversal, but replace the queue with a
stack.
• Algorithm using stack:
stack.???(the starting vertex);
while(!stack.isEmpty())
vertex = stack.???();
vertex.???();
for each unvisited vertex, next, adjacent to vertex,
stack.???(next);

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 48


Depth-First Traversal (DFS)
• Recursive algorithm:
dfs(vertex) {
vertex.???();
for each unvisited vertex, next, adjacent to vertex,
???(next);
}

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 49


Graph Traversals
• Trace a depth-first search on the following graph
from 9:

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 50


DFS: Example
S

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.

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 58


BFS vs. DFS

BFS DFS

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 59


BFS vs. DFS

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 60


BFS vs. DFS

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 61


BFS vs. DFS
BFS DFS
BFS Stands for “Breadth First Search”. DFS stands for “Depth First Search”.
BFS starts traversal from the root node and then DFS starts the traversal from the root node and
explore the search in the level by level manner i.e. as explore the search as far as possible from the root
close as possible from the root node. node i.e. depth wise.
Breadth First Search can be done with the help Depth First Search can be done with the help
of queue i.e. FIFO implementation. of stack i.e. LIFO implementations.
This algorithm works in single stage. The visited This algorithm works in two stages – in the first stage
vertices are removed from the queue and then the visited vertices are pushed onto the stack and
displayed at once. later on when there is no vertex further to visit those
are popped-off.
BFS is slower than DFS. DFS is much faster than BFS.
BFS requires more memory compare to DFS. DFS require less memory compare to BFS.
Applications of BFS Applications of DFS
> To find Shortest path > Useful in Cycle detection
> Single Source & All pairs shortest paths > In Connectivity testing
> In Spanning tree > Finding a path between V and W in the graph.
> In Connectivity > useful in finding spanning trees & forest.
BFS is useful in finding shortest path. BFS can be DFS in not so useful in finding shortest path. It is
used to find the shortest distance between some used to perform a traversal of a general graph and
starting node and the remaining nodes of the graph. the idea of DFS is to make a path as long as
possible, and then go back (backtrack) to add
branches also as long as possible.
Java Foundations, 4th Edition, Lewis/DePasquale/Chase Source: http://freefeast.info/difference-between/difference-between-bfs-and-dfs-bfs-vs-dfs/ 24 - 62
Any good
Connectivity idea?

• A graph is connected if and only if for each vertex


v in a graph containing n vertices, the size of the
result of a breadth-first traversal at v is n.

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 63


Spanning Trees
• A spanning tree is a tree that includes all of the
vertices of a graph and some, but possibly not all,
of the edges.
• Since trees are also graphs, for some graphs, the
graph itself will be a spanning tree, and thus the
only spanning tree for that graph will include all of
the edges.
• A spanning tree of a graph is connected & acyclic.

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 64


Spanning Tree
• An example of a spanning tree:

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 65


Spanning Tree

The graph on the right is a spanning tree of the graph on


the left

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 66


Spanning Tree
• Examples of a spanning tree:

Graph A Some Spanning Trees from Graph A

or or or

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 67


Complete Graph All 16 of its Spanning Trees

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.

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 69


Minimum Spanning Tree
• The Minimum Spanning Tree for a given graph is
the Spanning Tree of minimum cost for that
graph.
Complete Graph Minimum Spanning Tree

2 2
5 3 3

1 1

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 70


Minimum Spanning Tree
• Minimal Spanning Tree algorithm overview
– pick an arbitrary starting vertex and add it to our MST
– add all of the edges that include our starting vertex to
a minheap ordered by weight.
– remove the minimum edge from the minheap and
add the edge and the new vertex to our MST.
– add to the minheap all of the edges that include this
new vertex and whose other vertex is not already in
the MST.
– continue until the minheap is empty or all vertices
are in the MST.

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 71


Minimum Spanning Tree
Complete Graph

7
a b a
2
5 3

4
c 1 d

(a,d,2), (a,c,5), (a,b,7)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 72


Minimum Spanning Tree
Complete Graph

7
a b a
2
5 3

4
c 1 d d

(a,c,5), (a,b,7), (d,c,1), (d,b,3)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 73


Minimum Spanning Tree
Complete Graph

7
a b a
2
5 3

4
c 1 d c d

(a,c,5), (a,b,7), (d,b,3), (c,b,4)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 74


Minimum Spanning Tree
Complete Graph

7
a b a b
2
5 3

4
c 1 d c d

(a,c,5), (a,b,7), (c,b,4)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 75


Shortest Path
• There are two possibilities for determining the
“shortest” path in a graph.
– determine the literal shortest path between a starting vertex
and a target vertex (the least number of edges between the
two vertices)
• simplest approach – a variation of the breadth-first traversal algorithm
– look for the cheapest path in a weighted graph
• use a minheap or a priority queue storing vertex, weight pairs

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 76


Dijkstra’s Algorithm
• Dijkstra’s algorithm finds the shortest path from
one vertex v0 to each other vertex in a digraph.
• When it has finished, the length of the shortest
distance from v0 to v is stored in the vertex v, and
the shortest path from v0 to v is recorded in the
back pointers of v and the other vertices along
that path.
• The algorithm uses a priority queue, initializing it
with all the vertices and then dequeueing one
vertex on each iteration.
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 77
Dijkstra’s Shortest Paths Algorithm
• Precondition: G = (V,w) is a weighted graph with
initial vertex v0.
• Postcondition: Each vertex v in V stores the
shortest distance from v0 to v and a back
reference to the preceding vertex along that
shortest path.

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 78


Vertex<T>
- double gvalue
- T content
- Vertex from
- boolean visited
+ Vertex(T content)
+ getContent(): T
+ getGvalue(): double
+ setGvalue(double gvalue): void
+ getFrom(): Vertex
+ setFrom(Vertex vertex): void
+ visited(): void
+ isVisisted(): boolean

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 79


PriorityQueue queue = new PriorityQueue();

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);
}

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 80


// construct the path from startVertex to goalVertex
// using a stack

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 81


Dijkstra’s Shortest Paths Algorithm
• Dijkstra's algorithm to find the shortest path between a and b. It
picks the unvisited vertex with the lowest distance, calculates the
distance through it to each unvisited neighbor, and updates the
neighbor's distance if smaller. Mark visited (set to red) when done
with neighbors.

Java Foundations, 4th Edition, Lewis/DePasquale/Chase Source: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm 24 - 82


• From A to H

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 83


Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 84
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 85
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 86
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 87
• From A to H
Visited Queue
(null,A,0)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 88


• From A to H
Visited Queue
(null,A,0)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 89


• From A to H
Visited Queue
(null,A,0) (A,B,4), (A,C,6), (A,D,1)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 90


• From A to H
Visited Queue
(null,A,0) (A,B,4), (A,C,6)
(A,D,1)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 91


• From A to H
Visited Queue
(null,A,0) (A,B,4), (D,C,5), (D,F,3), (D,G,7)
(A,D,1)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 92


• From A to H
Visited Queue
(null,A,0) (A,B,4), (D,C,5), (D,G,7)
(A,D,1)
(D,F,3)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 93


• From A to H
Visited Queue
(null,A,0) (A,B,4), (F,C,4), (F,G,6), (F,H,8)
(A,D,1)
(D,F,3)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 94


• From A to H
Visited Queue
(null,A,0) (F,C,4), (F,G,6), (F,H,8)
(A,D,1)
(D,F,3)
(A,B,4)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 95


• From A to H
Visited Queue
(null,A,0) (F,C,4), (F,G,6), (F,H,8), (B,E,9)
(A,D,1)
(D,F,3)
(A,B,4)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 96


• From A to H
Visited Queue
(null,A,0) (F,G,6), (F,H,8), (B,E,9)
(A,D,1)
(D,F,3)
(A,B,4)
(F,C,4)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 97


• From A to H
Visited Queue
(null,A,0) (F,G,6), (F,H,8), (C,E,6)
(A,D,1)
(D,F,3)
(A,B,4)
(F,C,4)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 98


• From A to H
Visited Queue
(null,A,0) (F,H,8), (C,E,6)
(A,D,1)
(D,F,3)
(A,B,4)
(F,C,4)
(F,G,6)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 99


• From A to H
Visited Queue
(null,A,0) (F,H,8), (C,E,6)
(A,D,1)
(D,F,3)
(A,B,4)
(F,C,4)
(F,G,6)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 100


• From A to H
Visited Queue
(null,A,0) (F,H,8)
(A,D,1)
(D,F,3)
(A,B,4)
(F,C,4)
(F,G,6)
(C,E,6)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 101


• From A to H
Visited Queue
(null,A,0) (E,H,7)
(A,D,1)
(D,F,3)
(A,B,4)
(F,C,4)
(F,G,6)
(C,E,6)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 102


• From A to H
Visited Queue
(null,A,0)
(A,D,1)
(D,F,3)
(A,B,4)
(F,C,4)
(F,G,6)
(C,E,6)
(E,H,7)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 103


• From A to H
Visited Stack
(null,A,0) (null,A,0)
(A,D,1) (A,D,1)
(D,F,3) (D,F,3)
(A,B,4) (F,C,4)
(F,C,4) (C,E,6)
(F,G,6) (E,H,7)
(C,E,6)
(E,H,7)

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 104


• From A to H
Visited Stack
(null,A,0) (null,A,0)
(A,D,1) (A,D,1)
(D,F,3) (D,F,3)
(A,B,4) (F,C,4)
(F,C,4) (C,E,6)
(F,G,6) (E,H,7)
(C,E,6)
(E,H,7)

A -> D -> F -> C -> E -> H


Total cost: 7

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 105


Dijkstra’s Shortest Paths Algorithm

Java Foundations, 4th Edition, Lewis/DePasquale/Chase Source: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm 24 - 106


Dijkstra’s Shortest Paths Algorithm

Java Foundations, 4th Edition, Lewis/DePasquale/Chase Source: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm 24 - 107


Dijkstra’s Shortest Paths Algorithm

Java Foundations, 4th Edition, Lewis/DePasquale/Chase Source: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm 24 - 108


Implementing Graphs
• Strategies for implementing graphs:
• Adjacency matrices
– use a two dimensional array
– each position of the array represents an intersection between
two vertices in the graph
– each intersection is represented by a cost (or weight) value
indicating whether or not the two vertices are connected

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 109


Summary
• An undirected graph is a graph where the pairings representing the
edges are unordered.
• Two vertices in a graph are adjacent if there is an edge connecting
them.
• An undirected graph is considered complete if it has the maximum
number of edges connecting vertices.
• A path is a sequence of edges that connects two vertices in a graph.
• A cycle is a path in which the first and last vertices are the same and
none of the edges are repeated.
• An undirected tree is a connected, acyclic, undirected graph with
one element designated as the root.
• A directed graph, sometimes referred as a digraph, is a graph where
the edges are ordered pairs of vertices.
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 110
Summary
• A path in a directed graph is a sequence of directed edges that
connects two vertices in a graph.
• A network, or a weighted graph, is a graph with weights or costs
associated with each edge.
• The only difference between a depth-first traversal of a graph and a
breadth-first traversal is that the depth-first traversal uses a stack
instead of a queue to manage the traversal.
• A graph is connected if and only if the number of vertices in the
breadth-first traversal is the same as the number of vertices in the
graph, regardless of the starting vertex.
• A spanning tree is a tree that includes all of the vertices of a graph
and some, but possibly not all, of the edges.
• A minimum spanning tree 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.

Java Foundations, 4th Edition, Lewis/DePasquale/Chase 24 - 111

Potrebbero piacerti anche