Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 7 - Monday CS 113.

Similar presentations


Presentation on theme: "Week 7 - Monday CS 113."— Presentation transcript:

1 Week 7 - Monday CS 113

2 Last time What did we talk about last time? Stable marriage Lab 6

3 Questions?

4 Project 2

5 Eulerian Paths and Cycles
Cross that bridge when you come to it

6 Königsberg Used to be Königsberg, Prussia
Now called Kaliningrad, Russia On the Pregel River, including two large islands

7 Seven Bridges of Königsberg
In 1736, the islands were connected by seven bridges In modern times, there are only five

8 The Challenge After a lazy Sunday and a bit of drinking, the citizens would challenge each other to walk around the city and try to find a path which crossed each bridge exactly once

9 Can you find a solution? Can you find such a solution?
Start anywhere and find a path which crosses each bridge exactly once

10 Euler’s Solution What did Euler find? The same thing you did: nothing
But, he also proved it was impossible Here’s how: Center Island North Shore East South Shore Remarkable for having his name mispronounced and getting a number named after himself, also knew something about civil engineering.

11 Graph theoretical view
By simplifying the problem into a graph, the important features are clear To arrive as many times as you leave, the degrees of each node must be even (except for the starting and ending points) Center Island North Shore East South Shore

12 Eulerian Path Algorithm
There is actually an way to find such a path on a graph where one exists: Start with a node of odd degree if there is one Every time we move across an edge, delete it If you have a choice, always pick an edge whose deletion will not disconnect the graph At the end of the algorithm, you will either have an Eulerian path or an Eulerian cycle, depending on the graph

13 The Two Princes and the Bishop
We can label the nodes to make a more interesting problem Now each piece of land is associated with the Blue Prince, the Red Prince, the Bishop, or the Tavern

14 The Blue Prince: 8th Bridge
The Blue Prince wants to build an 8th bridge so that he can walk starting at his castle, cross every bridge once, and end at the Tavern to brag

15 The Blue Prince: Solution
Put the bridge from the Bishop’s land to the Red Prince’s land

16 The Red Prince: 9th Bridge
Furious, the Red Prince wants to build his own bridge so that only he can start at his own castle, cross all the bridges once and then end at the Tavern

17 The Red Prince: Solution
Put the bridge from the Red Prince’s land to the Blue Prince’s land

18 The Bishop: 10th Bridge Upset by this pettiness, the Bishop decides to build a 10th bridge which allows all citizens to cross all bridges and return to their starting point

19 The Bishop: Solution Put the bridge from the Center Island to the Red Prince’s land, making all pieces of land have even degree

20 Minimum Spanning Tree

21 What if… An airline has to stop running direct flights between some cities But, it still wants to be able to reach all the cities that it can now What’s the set of flights with the lowest total cost that will keep all cities connected? Essentially, what’s the lowest cost tree that keeps the graph connected?

22 Minimum spanning tree This tree is called the minimum spanning tree or MST It has countless applications in graph problems How do we find such a thing?

23 Prim’s Algorithm Start with two sets, S and V:
S has the starting node in it V has everything else Find the node u in V that is closest to any node in S Put the edge to u into the MST Move u from V to S If V is not empty, go back to Step 2

24 MST Example 9 A B 5 3 C 4 1 2 8 3 D E 3 5 2 2 F G

25 Google Maps How does Google Maps find the shortest route from Los Angeles to Elizabethtown Graph theory, of course! It stores a very large graph where locations are nodes and streets (well, parts of streets) are edges

26 Shortest Paths Let’s think more about weight on edges
Weight can represent time, distance, cost: anything, really The shortest path (lowest total weight) is not always obvious 6 B 5 2 D 8 3 4 C A 16 E

27 What’s the shortest path?
Take a moment and try to find the shortest path from A to E The shortest path has length 14 6 B B A 5 2 D E D C 8 3 4 C A 16 E

28 How can we always find the shortest path?
On a graph of that size, it isn’t hard to find the shortest path A Google Maps graph has millions and millions of nodes How can we come up with an algorithm that will always find the shortest path from one node to another?

29 Dijkstra’s Algorithm Start with two sets, S and V:
S has the starting node in it V has everything else Set the distance to all nodes in V to ∞ Find the node u in V that is closest to a node in S For every neighbor v of u in V If d(v) > d(u) + d(u,v) Set d(v) = d(u) + d(u,v) Move u from V to S If V is not empty, go back to Step 2

30 Finding the shortest distance from A to all other nodes
Example for Dijkstra Node d(u) A B 5 C 7 D 10 E 14 Node d(u) A B 5 C 8 D E 16 Node d(u) A B 5 C 7 D 10 E 16 Node d(u) A B 5 C 7 D 11 E 16 Node d(u) A B 5 C 7 D 10 E 16 Sets S V A, B, C D, E Sets S V A, B C, D, E Sets S V A, B, C, D E Sets S V A B, C, D, E Sets S V A, B, C, D, E Finding the shortest distance from A to all other nodes 6 B 5 2 D 8 3 4 C A 16 E

31 Features of Dijkstra Always gets the next closest node, so we know there isn’t a better way to get there Finds the shortest path from a starting node to all other nodes Works even for directed graphs

32 Software Engineering

33 Software development Often goes through phases similar to the following: Understand the problem Plan a solution to the problem Implement the solution in a programming language Test the solution Maintain the solution and do bug fixes Factor of 10 rule!

34 Mid-Semester Evaluations

35 Upcoming

36 Next time… Therac 25 Testing Programming languages

37 Reminders Keep working on Project 2 Read Python Chapter 7
Due Friday before midnight Read Python Chapter 7 Think about what you want to do for your Final Project


Download ppt "Week 7 - Monday CS 113."

Similar presentations


Ads by Google