Lab 12 PowerPoint

Download Report

Transcript Lab 12 PowerPoint

CIS121-204 – Fall 2007
Lab 12 – Last Lab
Tuesday, December 4, 2007
Course Evaluation
Importance of Evaluation


They tell me to read this.
Penn Engineering takes teaching quality very seriously. Therefore,
please take the time to give a candid and serious response to each of
the questions on this form. USE A NUMBER 2 PENCIL. The
information you supply is used in three ways: (a) by the instructor, to
help improve the quality of the course in subsequent years; (b) by the
Department and School to evaluate the quality of instruction for
purposes of rewarding excellent instructors; and (c) FOR
UNDERGRADUATES, by the Penn Course Review, a student publication
providing advice to students.
It is especially important that you take the time to give written
comments. Remember that your comments are anonymous and that
these forms are taken directly to the department office by a designated
student; the instructor receives the information only after the grades
for the course have been submitted.
Graph




Graph G=(V,E) consists of a set of
vertices V and a set of edges E, where
E is a subset of VxV.
vertex: singular
vertices: plural
G is simple if it contains no self loops or
parallel edges.
Multigraph is graph with parallel edges.
Graph: Example 1





Graph 1 is a multigraph with
a self loop at vertex 4.
Vertex 0 is adjacent to
vertices 1 and 2.
Graph 1 is not connected; it
has two connected
components: {0,1,2,3} and
{4}.
Edges of Graph 1 are
weighted because each edge
has a “weight.”
Graph 1 is undirected
because edges have no
directions.
0
100
2
2550
2547
2007
1
14
3
11
4
Graph 1
V={0,1,2,3,4}
E={(0,1),(0,2),(0,2),(1,3),
(2,3),(4,4)}
Note that G1 is a
multigraph, so E is a
multiset (having duplicate
elements).
Graph: Example 2





Graph 2 is a directed graph
because edges have directions.
Graph 2 is simple because there
is at most one edge in the same
direction between any two
vertices.
Edges of Graph 2 are
unweighted—all edges have the
same “weight,” say 1.
Graph 2 is connected because
for any two vertices u and v,
there is a path from u to v or
from v to u.
If for any two vertices u and v,
there is a path from u to v and
from v to u, the graph is
strongly connected.
0
2
1
3
5
4
6
Graph 2
Adjacency Lists



If there is an edge from
u to v, then v is in the
adjacency list of u.
What’s the adjacency
lists of this graph?
The first few:
0: {2}
1: {0,4}
…
0
2
1
3
5
4
6
Single-Source Shortest Paths





Minimize the “cost” (or length) to go from a
“source” vertex to any other vertex.
The length of shortest path from a vertex to
itself is zero.
If there is no path from u to v, then the
length of shortest path from u to v is infinity.
Weighted graph: CIS320—Dijkstra’s Algorithm
Unweighted graph: Now.
Single-Source Shortest Path
on Unweighted, Directed Graphs




Input: G and source vertex u
Starting from u, try to expand the set
reachable vertices.
Have a queue of discovered but unprocessed
vertices.
For each element in the queue:



If a new vertex w is discovered, then know the
shortest path from u to w. Put w in the queue
If an old vertex is encountered, do nothing.
Let’s do an example.
SSSP on Unweighted Graph:
Example




Want to find the length
of shortest paths from
source vertex 0.
Assign the initial
lengths: 0 for vertex 0
and infinity otherwise
Enqueue 0 because we
already “discovered”
vertex 0 but has not
processed it.
That’s all for the first
step.
∞
0
∞
0
1
2
3
∞
Queue:
[0]
5
∞
4
6
∞
∞
SSSP on Unweighted Graph:
Example (cont.)



Dequeue: 0
Length: 0
Consider the
neighbor of 0

1
0
∞
0
1
2
3
2: undiscovered


Update length.
Enqueue 2.
∞
Queue:
[2]
5
∞
4
6
∞
∞
SSSP on Unweighted Graph:
Example (cont.)



Dequeue: 2
Length: 1
Consider the neighbor
of 2

0: discovered


∞
0
1
2
3
2
4
Do nothing.
3: undiscovered



1
0
Update length.
Enqueue 3.
5: undiscovered


Update length.
Enqueue 5.
2
Queue:
[3,5]
5
6
∞
∞
SSSP on Unweighted Graph:
Example (cont.)



Dequeue: 3
Length: 2
Consider the
neighbor of 3

3
0
1
2
3
2
4
1: undiscovered



1
0
Update length.
Enqueue 1.
4: undiscovered


Update length.
Enqueue 4.
2
Queue:
[5,1,4]
5
6
∞
3
SSSP on Unweighted Graph:
Example (cont.)



Dequeue: 5
Length: 2
Consider the
neighbor of 5

1
0
3
0
1
2
3
6: undiscovered


Update length.
Enqueue 6.
2
Queue:
[1,4,6]
5
2
4
6
3
3
SSSP on Unweighted Graph:
Example (cont.)



Dequeue: 1
Length: 3
Consider the
neighbor of 1

3
0
1
2
3
2
4
0: discovered


1
0
Do nothing.
4: discovered

Do nothing.
2
Queue:
[4,6]
5
6
3
3
SSSP on Unweighted Graph:
Example (cont.)



Dequeue: 4
Length: 3
Consider the
neighbor of 4


None.
Done.
1
0
3
0
1
2
3
2
Queue:
[6]
5
2
4
6
3
3
SSSP on Unweighted Graph:
Example (cont.)



Dequeue: 6
Length: 3
Consider the
neighbor of 6

0
1
2
3
2
4
Do nothing.
4: discovered


3
3: discovered


1
0
Do nothing.
5: discovered

Do nothing.
2
Queue:
[]
5
6
3
3
SSSP on Unweighted Graph:
Example (cont.)

Queue is empty.

0
3
0
1
Done Done.
1
2
3
2
Queue:
[]
5
2
4
6
3
3
SSSP on Unweighted Graph:
Algorithm





Array sp of length |V| initialized as infinity
sp[u]=0
//the shortest path from u to u has length 0
Queue Q
Q.enqueue(u)
while Q is not empty


s=Q.dequeue()
for each v such that (s,v) is an edge

if sp[s]+1<sp[v]



return sp
sp[v]=sp[s]+1
Q.enqueue(v)
Takeaways from CIS121


Using a correct data structure saves time.
Three steps in writing a program:





Designing
Implementing
Testing
You should spend most of your time in the
first and last steps.
Eclipse… please.
Real programmers don’t have a life???
What to do after CIS121?


You probably want to see my
handwriting on the whiteboard for the
last time in this course…
… because I will use a chalkboard for
the review session.
Time’s Up.




Thanks for the good time we have had.
Hope you enjoy the section. I always do.
See you in the review session and the final exam.
Reminder: Final Exam




Date:
Time:
Place:
Thursday, December 13, 2007
9-11AM
Skirkanich Hall Auditorium
Good luck.


See you around.
Feel free to (and please) say hi.