www.prism.gatech.edu

Download Report

Transcript www.prism.gatech.edu

CS 1371
COMPUTING FOR ENGINEERS
Programming in Matlab Environment
Dr. Mary Hudachek-Buswell
1
STACKS, QUEUES, AND
GRAPHS
Chapter 17
2
Stacks and Queues
• Fundamental data types.
• Value: collection of objects.
• Operations: insert, remove, iterate, test if empty.
• Intent is clear when we insert.
• Which item do we remove?
3
Stacks and Queues
Stack. Examine the item most recently added.
3 operations push, pop, top
LIFO = "last in first out
Queue. Examine the item least recently added.
2 operations enqueue, dequeue
FIFO = "first in first out“
4
Graphs
• A graph is a set of nodes or vertices and a
collection of edges or arcs that each connect a
pair of vertices.
5
Graph Notes
• A self-loop is an edge that connects a vertex to itself.
• Two edges are parallel if they connect the same pair of vertices.
• When an edge connects two vertices, we say that the vertices are adjacent to one another
and that the edge is incident on both vertices.
• The degree of a vertex is the number of edges incident on it.
• A path in a graph is a sequence of vertices connected by edges. A simple path is one with
no repeated vertices.
• A cycle is a path (with at least one edge) whose first and last vertices are the same.
A simple cycle is a cycle with no repeated edges or vertices (except the requisite repetition
of the first and last vertices).
• The length of a path or a cycle is its number of edges.
6
Search Algorithms
• Depth-First Search BFS -> Stack
• Breadth-First Search BFS -> Queue
• Dijkstra's Algorithm - Shortest path in a graph
• Given two vertices, p and q determine the path with
lowest length
7
Depth-First Search DFS
Problem Find a natural way to systematically visit every
vertex and every edge of a graph :
• Start from one vertex
• Move forward all along one path (do not pass through
a vertex already visited)
• When stuck, turn back until you can step forward to an
unvisited vertex
• DFS finds some path from source vertex v to target
vertex u.
8
DFS Pseudo Code
dfs(v)
visit(v)
for each neighbor w of v
if w is unvisited
dfs(w)
add edge vw to tree T
end
end
end
9
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
A
C
G
K
Output: G
10
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
A
O
C
G
K
Output: G O
11
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
A
C
G
K
Output: G O
12
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
A
J
C
G
K
Output: G O J
13
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
A
A
J
C
G
K
Output: G O J A
14
DFS Example
Traverse Graph G
O
stack
G
E
S
J
C
T
A
A
J
C
G
K
Output: G O J A C
15
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
A
A
J
C
G
K
Output: G O J A C
16
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
A
J
C
G
K
Output: G O J A C
17
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
A
K
J
C
G
K
Output: G O J A C K
18
DFS Example
Traverse Graph G
O
stack
G
E
S
J
E
T
A
K
J
C
G
K
Output: G O J A C K E
19
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
E
T
A
K
J
C
G
K
Output: G O J A C K E T
20
DFS Example
Traverse Graph G
O
stack
G
E
S
J
S
T
E
T
A
K
J
C
G
K
Output: G O J A C K E T S
21
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
E
T
A
K
J
C
G
K
Output: G O J A C K E T S
22
DFS Example
Traverse Graph G
O
stack
G
E
S
J
E
T
A
K
J
C
G
K
Output: G O J A C K E T S
23
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
A
K
J
C
G
K
Output: G O J A C K E T S
24
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
A
J
C
G
K
Output: G O J A C K E T S
25
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
A
C
G
K
Output: G O J A C K E T S
26
DFS Example
Traverse Graph G
O
stack
G
E
S
J
T
A
C
K
Output: G O J A C K E T S
27
Breadth-First Search BFS
Problem: It is another systematic way of visiting the
vertices of a graph G. Start from a vertex, step forward all
vertices adjacent to it, then step forward all vertices
adjacent to its sons,...
The Breadth-First Search algorithm is quite the same
algorithm as the iterative DFS, you simply replace the
stack with a queue
• BFS is classic method to find a path with the fewest
nodes from source vertex v to target vertex u.
28
BFS Pseudo Code
bfs(s)
initialize Q to be a queue with one element s
while Q not empty
take a node u from Q
if explored[u]=false then
set explored[u]= true
for each edge (u,v) adjacent to u
add v to Q
end
end
end
29
BFS Example
Traverse Graph G
O
queue
G
G
E
S
J
T
A
C
K
Output: G
30
BFS Example
Traverse Graph G
O
queue
G
E
S
J
T
A
C
K
Output: G
31
BFS Example
Traverse Graph G
O
queue
O
G
E
S
J
T
A
C
K
Output: G O
32
BFS Example
Traverse Graph G
O
queue
J
G
E
S
O
J
T
A
C
K
Output: G O J
33
BFS Example
Traverse Graph G
O
queue
J
G
E
S
J
T
A
C
K
Output: G O J
34
BFS Example
Traverse Graph G
O
queue
G
E
S
J
T
A
C
K
Output: G O J
35
BFS Example
Traverse Graph G
O
queue
A
G
E
S
J
T
A
C
K
Output: G O J A
36
BFS Example
Traverse Graph G
O
queue
C
G
E
S
A
J
T
A
C
K
Output: G O J A C
37
BFS Example
Traverse Graph G
O
queue
K
G
E
S
J
C
A
T
A
C
K
Output: G O J A C K
38
BFS Example
Traverse Graph G
O
queue
K
G
E
S
C
J
T
A
C
K
Output: G O J A C K
39
BFS Example
Traverse Graph G
O
queue
K
G
E
S
J
T
A
C
K
Output: G O J A C K
40
BFS Example
Traverse Graph G
O
queue
G
E
S
J
T
A
C
K
Output: G O J A C K
41
BFS Example
Traverse Graph G
O
queue
E
G
E
S
J
T
A
C
K
Output: G O J A C K E
42
BFS Example
Traverse Graph G
O
queue
G
E
S
J
T
A
C
K
Output: G O J A C K E
43
BFS Example
Traverse Graph G
O
queue
T
G
E
S
J
T
A
C
K
Output: G O J A C K E T
44
BFS Example
Traverse Graph G
O
queue
S
G
E
S
T
J
T
A
C
K
Output: G O J A C K E T S
45
BFS Example
Traverse Graph G
O
queue
S
G
E
S
J
T
A
C
K
Output: G O J A C K E T S
46
BFS Example
Traverse Graph G
O
queue
G
E
S
J
T
A
C
K
Output: G O J A C K E T S
47
Dijkstra’s Shortest Path
Problem Find the shortest path problem for weighted
directional graphs:
• Start from one vertex, choose stop vertex
• Move forward all along shortest path (do not pass
through a vertex already visited) using BFS
• Keep track of the distance of the path as compared to
other paths.
• If you do not reach stop vertex, the path is disregarded
48
Dijkstra’s Pseudo Code
Dijk(g)
initialize distance to source vertex, s, to zero
for all v ∈ V–{s}
set all other vertices’ distances to infinity
do dist[v] ←∞
create empty set, S, to hold the visited
populate the queue Q initially with all vertices
while Q is not empty
do select an element of Q with the min. distance), u
add u to list of visited vertices)
for all v ∈ neighbors[u]
do if dist[v] > dist[u] + w(u, v) (if new shortest path found)
then
d[v] ←d[u] + w(u, v)
(set new value of shortest path)
return dist
49
Dijkstra Animated Example
50
51
52
53
54
55
56
57
58
59
Shortest Path Tree from A to all other
nodes
60
Minimum Spanning Tree MST
A minimum spanning tree (MST) of an edgeweighted graph is a spanning tree that connects all
vertices in G and has a minimum total weight
• Concept: Let V be any subset of the vertices of G,
and let edge e be the smallest edge connecting V
to G-V. Then e is part of the minimum spanning
tree.
61
Minimum Spanning Tree MST
62
Minimum Spanning Tree MST
A minimum spanning tree (MST) of an edgeweighted graph is a spanning tree that connects all
vertices in G and has a minimum total weight
• Concept: Let V be any subset of the vertices of G,
and let edge e be the smallest edge connecting V
to G-V. Then e is part of the minimum spanning
tree.
63
MST Pseudo Code
Consider a weighted connected graph G with n vertices. Prim’s
algorithm finds a minimum spanning tree of G.
Prim(G)
T := a minimum-weight edge
for i = 1 to n − 2
e := an edge of minimum weight incident to a vertex in T, and
not forming a circuit in T if added to T
T := T with e added
end
return(T)
64
MST Example
add edge {d, e}, weight 1
add edge {c, e}, weight 2
add edge {d, z}, weight 2
add edge {b, e}, weight 3
add edge {a, b}, weight 2
Produces MST weight 10
65
MST Example
add edge {d, e}, weight 1
add edge {c, e}, weight 2
add edge {d, z}, weight 2
add edge {b, e}, weight 3
add edge {a, b}, weight 2
Produces MST weight 10
66