cis121-204 – fall 2007 lab 12 – last lab tuesday, december 4, 2007

21
CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

Post on 20-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

CIS121-204 – Fall 2007

Lab 12 – Last LabTuesday, December 4, 2007

Page 2: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

Course EvaluationImportance 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.

Page 3: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

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: singularvertices: plural

G is simple if it contains no self loops or parallel edges.

Multigraph is graph with parallel edges.

Page 4: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

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.

2 43

0 1

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).

2550

2007

1411

2547100

Page 5: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

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.

1

2

5

3

6

0

4

Graph 2

Page 6: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

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}…

1

2

5

3

6

0

4

Page 7: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

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.

Page 8: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

Single-Source Shortest Pathon 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.

Page 9: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

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.

1

2

5

3

6

0

4

Queue:

[0]

0 ∞

∞∞ ∞

∞ ∞

Page 10: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

SSSP on Unweighted Graph: Example (cont.)

Dequeue: 0 Length: 0 Consider the

neighbor of 0 2: undiscovered

Update length. Enqueue 2.

1

2

5

3

6

0

4

Queue:

[2]

0 ∞

∞1 ∞

∞ ∞

Page 11: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

SSSP on Unweighted Graph: Example (cont.) Dequeue: 2 Length: 1 Consider the

neighbor of 2 0: discovered

Do nothing. 3: undiscovered

Update length. Enqueue 3.

5: undiscovered Update length. Enqueue 5.

1

2

5

3

6

0

4

Queue:

[3,5]

0 ∞

∞1 2

2 ∞

Page 12: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

SSSP on Unweighted Graph: Example (cont.) Dequeue: 3 Length: 2 Consider the

neighbor of 3 1: undiscovered

Update length. Enqueue 1.

4: undiscovered Update length. Enqueue 4.

1

2

5

3

6

0

4

Queue:

[5,1,4]

0 3

31 2

2 ∞

Page 13: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

SSSP on Unweighted Graph: Example (cont.)

Dequeue: 5 Length: 2 Consider the

neighbor of 5 6: undiscovered

Update length. Enqueue 6.

1

2

5

3

6

0

4

Queue:

[1,4,6]

0 3

31 2

2 3

Page 14: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

SSSP on Unweighted Graph: Example (cont.) Dequeue: 1 Length: 3 Consider the

neighbor of 1 0: discovered

Do nothing. 4: discovered

Do nothing.

1

2

5

3

6

0

4

Queue:

[4,6]

0 3

31 2

2 3

Page 15: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

SSSP on Unweighted Graph: Example (cont.)

Dequeue: 4 Length: 3 Consider the

neighbor of 4 None. Done.

1

2

5

3

6

0

4

Queue:

[6]

0 3

31 2

2 3

Page 16: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

SSSP on Unweighted Graph: Example (cont.) Dequeue: 6 Length: 3 Consider the

neighbor of 6 3: discovered

Do nothing. 4: discovered

Do nothing. 5: discovered

Do nothing.

1

2

5

3

6

0

4

Queue:

[]

0 3

31 2

2 3

Page 17: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

SSSP on Unweighted Graph: Example (cont.)

Queue is empty. Done Done.

1

2

5

3

6

0

4

Queue:

[]

0 3

31 2

2 3

Page 18: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

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] sp[v]=sp[s]+1 Q.enqueue(v)

return sp

Page 19: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

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???

Page 20: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

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.

Page 21: CIS121-204 – Fall 2007 Lab 12 – Last Lab Tuesday, December 4, 2007

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: Thursday, December 13, 2007 Time: 9-11AM Place: Skirkanich Hall Auditorium

Good luck. See you around. Feel free to (and please) say hi.