Decision Maths: 3. Algorithms On Graphs Flashcards
What is a minimum spanning tree
A minimum spanning tree is a tree of minimum total weight that connects all of the nodes.
Method for Kruskal’s Algorithm
- List the edges in order of weight, smallest first.
- Choose the edge with the smallest weight.
- From the remaining edges, choose the edge with the smallest weight, as long as it does not form a cycle with the edges already chosen.
- Repeat step 3 until all the vertices have been included.
(5. Then add all the edge weights together to find a total weight).
Method for Prim’s Algorithm
- Choose a start vertex
- Connect the start vertex to the nearest new vertex by adding the shortest edge.
- From any vertex on the tree so far, add the shortest edge that connects a new vertex
- Repeat step 3 until all the vertices have been included.
(5. Then add the weights of each of the edges to find the total weight.)
Advantages and Disadvantages of Kruskal’s and Prim’s
- Kruskal’s algorithm is very intuitive but it takes quite a lot of a work to sort the edges before you start.
- In a large network checking for cycles can be tricky
- Prim’s algorithm is also quite intuitive and is easier to use on a large network
Method for Prim’s Algorithm for a Matrix
- Choose a start vertex. Label the column corresponding to that vertex with a 1 and cross out / delete that row.
- Look in all columns that have been numbered so far. Circle the smallest undeleted entry and read off the vertex for the row.
- Label the column corresponding to this vertex with the next available label then delete the row.
- Repeat steps 2 and 3 until all rows have been deleted
(5. Draw out the corresponding graph while doing steps 2 and 3)
(6. Then add the weights of all the arcs to find the total weight of the graph)
Method for doing Dijkstra’s Algorithm for labelling the vertices (find the min distance/time)
Step 1:
Give the start vertex (S) a permanent label of 0.
Step 2:
Give each vertex connected to S a working value by recording its distance from S.
Step 3:
Find the smallest working value and give the corresponding vertex V this value as a permanent label.
Step 4:
Update working values at any unlabelled vertices that can be reached from V.
Step 5:
Repeat steps 3 and 4 until the destination vertex has been given a permanent label.
What happens if 2 working values are joint smallest (which one do you choose)
If 2 working values are joint smallest, it doesn’t matter which one you choose.
Method for doing Dijkstra’s Algorithm for Backtracking (finding the route)
Step 1:
Start from the destination vertex.
Step 2:
From the current vertex V, look at at the vertices that lead directly to V.
Step 3:
Of these vertices, vertex P is the previous vertex on the route if ‘Label at V’ - ‘Label at P’ = ‘Weight (PV)’
Step 4:
Repeat steps 2 and 3 using the vertex just found as the current vertex
Step 5:
Stop when you have backtracked all the way through the graph to the start vertex .
(Remember to flip the order of the vertices so it goes from start to finish)
What does Floyd’s Algorithm do
Floyd’s algorithm is used to find the shortest route between any pair of vertices on a graph
What does Dijkstra’s Algorithm do
Dijkstra’s algorithm only finds the shortest route between a start node and the rest.