Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discrete Math for Computer Science. Mathematical Model Real-world Problem Computerized Solution Abstract Model Transformed Model picture of the real worldpicture.

Similar presentations


Presentation on theme: "Discrete Math for Computer Science. Mathematical Model Real-world Problem Computerized Solution Abstract Model Transformed Model picture of the real worldpicture."— Presentation transcript:

1 Discrete Math for Computer Science

2 Mathematical Model Real-world Problem Computerized Solution Abstract Model Transformed Model picture of the real worldpicture of the computer solution

3 Example Six Scottish towns – Aberdeen, Edinburgh, Fort William, Glasgow, Inverness and Perth are connected by roads. Find a road network of minimal length connecting all towns.

4 Real-World Model

5 Transformed Model Aberdeen Edinburgh Perth Inverness Glasgow Fort William 168 147 105 157 42 61 66 108 147 112 132 81 45 107 120

6 Graph A graph is a collection of vertices and edges. A weighted graph is a graph with weights assigned to each edge. A directed graph is a graph whose edges are arrows. A connected graph is a graph with a path between any two vertices. G = (V,E)‏ Example: V = { Aberdeen, Edinburgh, Perth, Fort William, Glasgow, Inverness } E = { (Aberdeen, Edinburgh), (Aberdeen, Perth),..., (Inverness, Glasgow) } Exercise: What kind of graph is ours?

7 Computerized Solution: An algorithm is an unambiguous sequence of instructions that yields a solution in finite time. Prim's Algorithm for a Minimum Connecting Path of Weighted Graph 1) Select any vertex and connect it to its nearest neighbour. 2) Find an unconnected vertex that is closest to the previously connected vertices and connect it 3) Repeat Step 2 until all vertices are connected

8 Prim's Algorithm in Action Aberdeen Edinburgh Perth Inverness Glasgow Fort William 168 147 105 157 42 61 66 108 147 112 132 81 45 107 120

9 Prim's Algorithm in Action Aberdeen Edinburgh Perth Inverness Glasgow Fort William 168 147 105 157 42 61 66 108 147 112 132 81 45 107 120

10 Prim's Algorithm in Action Aberdeen Edinburgh Perth Inverness Glasgow Fort William 168 147 105 157 42 61 66 108 147 112 132 81 45 107 120

11 Prim's Algorithm in Action Aberdeen Edinburgh Perth Inverness Glasgow Fort William 168 147 105 157 42 61 66 108 147 112 132 81 45 107 120

12 Prim's Algorithm in Action Aberdeen Edinburgh Perth Inverness Glasgow Fort William 168 147 105 157 42 61 66 108 147 112 132 81 45 107 120 Total Distance: 339 miles

13 Observation: This algorithm would work just as well if applied to six towns in New York. It wouldn't be very useful if we wanted to drive the shortest route because we would have to back track. It would be a good algorithm if we wanted to pave at least one road into each town, have all towns connected somehow by paved road and wanted to pave as little as possible.

14 Exercises: What does Prim's Algorithm call the “minimum connecting path” we have created? Find the minimum connecting path in the following:

15 Expressing Algorithms in Pseudocode: Pseucocode is a combination of English and programing language structures. begin Sequence of English language descriptions end if condition Sequence of English language descriptions else Sequence of English language descriptions repeat Sequence of English language descriptions until condition SEQUENCE ITERATION SELECTION NOTE: The above can be expressed as a for-loop for all elements in a described set do something

16 Examples: begin input n; if n < 0 n = -1*n output n end Exercise: What does this algorithm do? begin sum := 0; for i:= 1 to n do begin j := i*i; sum := sum + j; end output sum end Exercise: What does this algorithm do?

17 Prim's Algorithm: begin v := any starting vertex; u := nearest adjacent vertex; Connect u and v; while unconnected vertices remain do begin u:= an unconnected vertex nearest a connected vertex; Connect u to the nearest connected vertex end

18 Homework Do all the exercises at the end of the chapter  1: Make your output a sequence of edges in the order they are added to the minimum connecting path.  2, 3, 5, 6: make sure your description is in everyday english and not pseudocode itself  5, 6: the trace should be in the form of a table with columns labeled by variables and lines in the table correspond to individual lines of code after execution of that line. Number the lines of code in the algorithm.  6: replace “decreasing” with “increasing”


Download ppt "Discrete Math for Computer Science. Mathematical Model Real-world Problem Computerized Solution Abstract Model Transformed Model picture of the real worldpicture."

Similar presentations


Ads by Google