- 67 Views
- Uploaded on
- Presentation posted in: General

CIS121-204 – Fall 2007

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

CIS121-204 – Fall 2007

Lab 12 – Last Lab

Tuesday, December 4, 2007

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

2550

0

1

100

2547

14

11

2007

3

4

2

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 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 vor from v to u.
- If for any two vertices u and v, there is a path from u to vand from v to u, the graph is strongly connected.

0

1

2

3

4

5

6

Graph 2

- 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

1

2

3

4

5

6

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

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

0

∞

- 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

1

∞

∞

∞

2

3

4

5

6

∞

∞

Queue:

[0]

0

∞

- Dequeue: 0
- Length: 0
- Consider the neighbor of 0
- 2: undiscovered
- Update length.
- Enqueue 2.

- 2: undiscovered

0

1

∞

1

∞

2

3

4

5

6

∞

∞

Queue:

[2]

0

∞

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

- 0: discovered

0

1

2

1

∞

2

3

4

5

6

2

∞

Queue:

[3,5]

0

3

- Dequeue: 3
- Length: 2
- Consider the neighbor of 3
- 1: undiscovered
- Update length.
- Enqueue 1.

- 4: undiscovered
- Update length.
- Enqueue 4.

- 1: undiscovered

0

1

2

1

3

2

3

4

5

6

2

∞

Queue:

[5,1,4]

0

3

- Dequeue: 5
- Length: 2
- Consider the neighbor of 5
- 6: undiscovered
- Update length.
- Enqueue 6.

- 6: undiscovered

0

1

2

1

3

2

3

4

5

6

2

3

Queue:

[1,4,6]

0

3

- Dequeue: 1
- Length: 3
- Consider the neighbor of 1
- 0: discovered
- Do nothing.

- 4: discovered
- Do nothing.

- 0: discovered

0

1

2

1

3

2

3

4

5

6

2

3

Queue:

[4,6]

0

3

- Dequeue: 4
- Length: 3
- Consider the neighbor of 4
- None.
- Done.

0

1

2

1

3

2

3

4

5

6

2

3

Queue:

[6]

0

3

- Dequeue: 6
- Length: 3
- Consider the neighbor of 6
- 3: discovered
- Do nothing.

- 4: discovered
- Do nothing.

- 5: discovered
- Do nothing.

- 3: discovered

0

1

2

1

3

2

3

4

5

6

2

3

Queue:

[]

0

3

- Queue is empty.
- Done Done.

0

1

2

1

3

2

3

4

5

6

2

3

Queue:

[]

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

- if sp[s]+1<sp[v]

- return sp

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

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

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