Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.

Similar presentations


Presentation on theme: "Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential."— Presentation transcript:

1 Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential. If ‘weights’ are attached to the paths (to describe flows, times or distances) then the graph is called a ‘Network’.

2 Graph Vocabulary AB CD A graph is made up of nodes (dots) and arcs (lines/curves). It can contain multiple arcs and loops. A graph without any loops or multiple arcs is called simple. In a graph arcs are generally ‘two way’. A graph with at least one directed arc is called a di-graph. A subgraph is a small part of a graph, a subset of arcs and nodes AB CD AB D

3 Since graphs are only used to describe how thing are connected together, the lengths and positions of the arcs are not important. This means that graphs relaying the same information can be drawn in many different ways. We call two graphs that are really the same Isomorphic. Which of these graphs are isomorphic? How can you easily tell?

4 We can also try and capture the information in the Graph using a matrix. When written in this form it is much easier to spot if two graphs are isomorphic Clearly it can be difficult to tell if two graphs are isomorphic. To make life easier we attempt to draw graphs so that none of the arcs cross, this isn’t always possible, but graphs for which it is possible are called planar. This graph is planar because we can draw it like this with no arcs crossing A B C D ABCDABCD ABCDABCD A B C D A B C D

5 1) Look at the diagram for a circuit board opposite. Draw a graph to show how the six components are ‘directly’ connected to each other. 2) Represent the graphs below with a matrix. 3) Decide which of the graphs below are planar A B C D E A B C D E A B C D A B D C What do you notice about the matrices for i), ii) and iv) compared to iii)? Why is this the case? i) ii)iii) iv) i) ii)iii) iv)

6 AB CD A complete graph is one where every node is directly connected to every other node. This is the graph of K 4. A B C D F G E A bi-partite graph is a graph where the nodes split into 2 sets so that nodes in each set are not connected to other nodes in the same set. Types of Graphs and Networks

7 AB CD A walk/route around a graph/network is a sequence of linked arcs Eg. A – B – D – B – D – C A walk/route is closed if it starts and ends at the same node Eg. A – B – D – B – A A trail is a walk/route that doesn’t visit an edge more than once Eg. A – B – D – B (along alternate arc) A path is a trail that doesn’t visit a node more than once Eg. A – B – D A cycle is a path with the extra condition that the start and end nodes are the same Eg. B – D – B A Hamiltonian cycle is a cycle that visits every node (ie it will visit every node exactly once apart from the start/end node) Eg. A – B – C - D A graph/network is connected if every pair of nodes can be linked by a trail. The order/degree/valency of a node is the number of arcs that are incident to it Eg The order of A is 2, the order of B is 3, the order of C is 4

8 1)Is the complete graph with 6 nodes planar? 2) Look at the map opposite. Use the map opposite to determine the shortest distance between any two towns. Use this information to draw a graph representing the shortest distance between each of the towns. 3) Which of the graphs below contain a Hamiltonian cycle? 20 3 6 8 3 9 12 2 8 A B C D E F A B C D E F A B C D E F G H A B C D E F G

9 Lesson Objective: Trees, Spanning Trees and Minimum Spanning Trees

10 Look at these graphs. They are all ‘trees’. What do you think makes a graph a tree? Can you draw some ‘trees’ of your own?

11 A tree with is a connected graph that contains only trivial cycles. Draw 2 ‘different trees’ with 4 nodes Draw 2 ‘different trees’ with 5 nodes Draw 2 ‘different trees’ with 6 nodes What can you say about the number of arcs in a tree with ‘n’ nodes. Why? How many ‘different’ trees are there with 3 nodes? How many ‘different’ trees are there with 4 nodes? 5 nodes? Etc.

12 When you find a tree for a network it is often called a spanning tree because it is a way of linking the nodes of the network together using the smallest number of arcs. Find two ‘different’ spanning trees for the network below? Add up the weights of the arcs in your spanning trees are they the same? How many different spanning trees can you find? Which has the smallest total weight? The spanning tree with the smallest total weight for a particular network is called the minimum spanning tree. 10 7 6 8 5 5 3 A B C D E F G 9 4 6

13 Imagine the dots on this page represent underground stations. The stations need to be joined up using the smallest amount of track possible, but only by joining stations directly to other stations. The distances between each of the dots represents the ‘weight ‘ of the arc. What is the best solution?

14 Imagine the dots on this page represent underground stations. The stations need to be joined up using the smallest amount of track possible, but only by joining stations directly to other stations. The distances between each of the dots represents the ‘weight ‘ of the arc. What is the best solution?

15 Kruskal’s Algorithm (To find a minimum spanning tree) Step 1:Sort the edges in ascending order of weight Step 2:Select the edge of least weight Step 3:Select, from the edges not previously selected, the edge of least weight that does not form a cycle together with edges already included Step 4:Repeat step 3 until all the vertices are connected.

16 We model the situation as a network, then the problem is to find the minimum connector for the network A F B C D E 2 7 4 5 8 6 4 5 3 8

17 A F B C D E 2 7 4 5 8 6 4 5 3 8 List the edges in order of size: ED 2 AB 3 AE 4 CD 4 BC 5 EF 5 CF 6 AF 7 BF 8 CF 8 Kruskal’s Algorithm

18 Select the shortest edge in the network ED 2 Kruskal’s Algorithm A F B C D E 2 7 4 5 8 6 4 5 3 8

19 Select the next shortest edge which does not create a cycle ED 2 AB 3 Kruskal’s Algorithm A F B C D E 2 7 4 5 8 6 4 5 3 8

20 Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 (or AE 4) Kruskal’s Algorithm A F B C D E 2 7 4 5 8 6 4 5 3 8

21 Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 Kruskal’s Algorithm A F B C D E 2 7 4 5 8 6 4 5 3 8

22 Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 BC 5 – forms a cycle EF 5 Kruskal’s Algorithm A F B C D E 2 7 4 5 8 6 4 5 3 8

23 All vertices have been connected. The solution is ED 2 AB 3 CD 4 AE 4 EF 5 Total weight of tree: 18 Kruskal’s Algorithm A F B C D E 2 7 4 5 8 6 4 5 3 8

24 Prim’s Algorithm (To find a minimum spanning tree) Step 1:Choose a starting vertex Step 2:Choose the vertex ‘nearest’ to the starting vertex and the associated edge to the tree Step 3:Connect to the tree the vertex that is ‘nearest’ to any vertex in the connected set Step 4:Repeat step 3 until all the vertices are connected.

25 A F B C D E 2 7 4 5 8 6 4 5 3 8 Select any vertex A Select the shortest edge connected to that vertex AB 3 Prim’s Algorithm

26 A F B C D E 2 7 4 5 8 6 4 5 3 8 Select the shortest edge connected to any vertex already connected. AE 4 Prim’s Algorithm

27 Select the shortest edge connected to any vertex already connected. ED 2 Prim’s Algorithm A F B C D E 2 7 4 5 8 6 4 5 3 8

28 Select the shortest edge connected to any vertex already connected. DC 4 Prim’s Algorithm A F B C D E 2 7 4 5 8 6 4 5 3 8

29 Select the shortest edge connected to any vertex already connected. CB 5 – forms a cycle EF 5 Prim’s Algorithm A F B C D E 2 7 4 5 8 6 4 5 3 8

30 A F B C D E 2 7 4 5 8 6 4 5 3 8 All vertices have been connected. The solution is ED 2 AB 3 CD 4 AE 4 EF 5 Total weight of tree: 18

31 Apply both Prim’s and Kruskal’s algorithms to the networks below to find a minimum spanning tree. What is the weight of the minimum spanning tree in each case? 1)The algorithms will always find a minimum spanning tree, but do they always find the same minimum spanning tree? 2)Try and think of an advantage/disadvantage of each algorithm.

32 Apply both Prim’s and Kruskal’s algorithms to the networks below to find a minimum spanning tree. What is the weight of the minimum spanning tree in each case? 1)The algorithms will always find a minimum spanning tree, but do they always find the same minimum spanning tree? 2)Try and think of an advantage/disadvantage for each algorithm. A solution – there are others. Weight 22.A solution – there are others. Weight 28.

33 Both algorithms will always give solutions with the same length. They will usually select edges in a different order – you must show this in your workings. Occasionally they will use different edges – this may happen when you have to choose between edges with the same length. In this case there is more than one minimum connector for the network. Some points to note

34 The advantage of Prim’s algorithm is that you don’t have to check for cycles. This means that you can apply the algorithm easily without ‘seeing’ the network, just by having the matrix. This makes it ideal to program into a computer. Apply Prim’s algorithm to the network below, then write down a matrix for the network

35

36 PRIM’S ALGORITHM FOR A MATRIX Step 1: Start anywhere, say A. Cross out the row for this vertex and put an arrow over the corresponding column. Step 2: Look down all arrowed columns and find the smallest value that is not crossed out. Cross out the remainder of this row and put an arrow over the column corresponding to the row vertex. Step 3: Repeat step 2 until all columns have arrows

37 Write down a matrix for the network and use it to find a minimum spanning tree using the algorithm on the matrix. A   B  C D  E F  G 12 32 41 11 20 11 14 32 9 7

38 Network Matrix ABCDEFG A-12---41- B12-321120-- C-32--11-14 D-11--327- E-201132--9 F41--7--- G--14-9--

39 Prim’s algorithm in matrix form Step 1: Start anywhere, say A.Cross out the row for this vertex and put an arrow over the corresponding column. Step 2: Look down all arrowed columns and find the smallest value that is not crossed out. Cross out the remainder of this row and put an arrow over the column corresponding to the row vertex. Step 3: Repeat step 2 until all columns have arrows

40 ABCDEFG A-12---41- B12-321120-- C-32--11-14 D-11--327- E-201132--9 F41--7--- G--14-9-- 12 3 4 56 7

41 Apply Prim’s algorithm to these matrices:

42

43 Lesson Objective: Finding the shortest route between two nodes in a Network 4 3 7 1 4 2 4 7 2 5 32 A C D B F E G Look at the network below. If the weights represents the distances between the nodes, what is the shortest route from A to G?

44 Djikstra’s algorithm is an algorithm used to solve this kind of problem. It works by scanning the network from A to G labelling them one at a time in the search for an optimal path. It only permanently labels (or ‘boxes’) a node when it can guarantee that there is no shorter route to that node from the start position.

45 Dijkstra’s Algorithm finds the shortest path from the start vertex to every other vertex in the network. We will find the shortest path from A to G 4 3 7 1 4 2 4 7 2 5 32 A C D B F E G

46 Dijkstra’s Algorithm 1.Label the start vertex with permanent label 0 and order label 1 2Assign temporary labels to all the vertices that can be reached directly from the start 3Select the vertex with the smallest temporary label and make its label permanent. Add the correct order label. 4Put temporary labels on each vertex that can be reached directly from the vertex you have just made permanent. The temporary label must be equal to the sum of the permanent label and the direct distance from it. If there is an existing temporary label at a vertex, it should be replaced only if the new sum is smaller. 5Select the vertex with the smallest temporary label and make its label permanent. Add the correct order label. 6Repeat until the finishing vertex has a permanent label. 7To find the shortest paths(s), trace back from the end vertex to the start vertex. Write the route forwards and state the length.

47 Dijkstra’s Algorithm Order in which vertices are labelled. Distance from A to vertex Working A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 1 0 Label vertex A 1 as it is the first vertex labelled

48 Dijkstra’s Algorithm A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 4 3 7 We update each vertex adjacent to A with a ‘working value’ for its distance from A. 1 0

49 Dijkstra’s Algorithm A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 4 3 7 2 3 Vertex C is closest to A so we give it a permanent label 3. C is the 2 nd vertex to be permanently labelled. 1 0

50 Dijkstra’s Algorithm We update each vertex adjacent to C with a ‘working value’ for its total distance from A, by adding its distance from C to C’s permanent label of 3. 6 8 1 0 4 7 2 3 3 A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 6 < 7 so replace the t-label here

51 Dijkstra’s Algorithm 6 8 1 0 4 7 2 3 3 A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 The vertex with the smallest temporary label is B, so make this label permanent. B is the 3 rd vertex to be permanently labelled. 3 4

52 Dijkstra’s Algorithm 6 8 1 0 4 7 2 3 3 A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 3 4 We update each vertex adjacent to B with a ‘working value’ for its total distance from A, by adding its distance from B to B’s permanent label of 4. 5 8 5 < 6 so replace the t-label here

53 Dijkstra’s Algorithm 6 8 1 0 4 7 2 3 3 A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 3 4 5 8 The vertex with the smallest temporary label is D, so make this label permanent. D is the 4 th vertex to be permanently labelled. 4 5

54 Dijkstra’s Algorithm 6 8 1 0 4 7 2 3 3 A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 3 4 5 8 4 5 We update each vertex adjacent to D with a ‘working value’ for its total distance from A, by adding its distance from D to D’s permanent label of 5. 7 < 8 so replace the t-label here 12 7 7 < 8 so replace the t-label here 7

55 Dijkstra’s Algorithm 6 8 1 0 4 7 2 3 3 A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 3 4 5 8 4 5 12 7 7 The vertices with the smallest temporary labels are E and F, so choose one and make the label permanent. E is chosen - the 5 th vertex to be permanently labelled. 5 7

56 Dijkstra’s Algorithm 6 8 1 0 4 7 2 3 3 A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 3 4 5 8 4 5 12 7 7 5 7 We update each vertex adjacent to E with a ‘working value’ for its total distance from A, by adding its distance from E to E’s permanent label of 7. 9 < 12 so replace the t-label here 9

57 Dijkstra’s Algorithm 6 8 1 0 4 7 2 3 3 A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 3 4 5 8 4 5 12 7 7 5 7 The vertex with the smallest temporary label is F, so make this label permanent.F is the 6 th vertex to be permanently labelled. 9 6 7

58 Dijkstra’s Algorithm 6 8 1 0 4 7 2 3 3 A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 3 4 5 8 4 5 12 7 7 5 7 9 6 7 We update each vertex adjacent to F with a ‘working value’ for its total distance from A, by adding its distance from F to F’s permanent label of 7. 11 > 9 so do not replace the t-label here

59 Dijkstra’s Algorithm 6 8 1 0 4 7 2 3 3 A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 3 4 5 8 4 5 12 7 7 5 7 9 6 7 G is the final vertex to be permanently labelled. 7 9

60 Dijkstra’s Algorithm 6 8 1 0 4 7 2 3 3 A C D B F E G 4 3 7 1 4 2 4 7 2 5 32 3 4 5 8 4 5 12 7 7 5 7 9 6 7 7 9 To find the shortest path from A to G, start from G and work backwards, choosing arcs for which the difference between the permanent labels is equal to the arc length. The shortest path is ABDEG, with length 9.

61

62 First of all draw boxes as shown next to each of the nodes.

63 Text book page 12,13 and 11 in that order page 94-97 Ex 11E


Download ppt "Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential."

Similar presentations


Ads by Google