Graphs 1 Neil Ghani University of Strathclyde
Where are we …. We studied lists: * Searching and sorting a list Then we studied trees: * Efficient search of trees * BSTs, AVL Trees, Red Black Trees Now we study graphs: * Minimal Spanning Trees * Travelling Salesman
What is a graph A map has cities and roads between them A process has states and actions between them A graph has … * A set V of vertexes * A set E of edges. There are functions start: E -> V finish : E -> V distance : E -> Nat
Two graph algorithms Minimal spanning tree * find a set of edges connecting all vertices * with minimal total distance Travelling salesman problem * find a cycle (route) through the graph * with minimal total distance
Minimal Spanning Tree Kruskal’s algortihm solves constructs a minimal spanning tree Create a set S containing all the edges Create a set F of all vetices Remove an element of S with least weight. If the edge connects two trees in F, replace these two trees by the new connected tree Stop when there is only one tree in F
Complexity of Kurskal What is the complexity of Kruskal’s algorithm. * To choose the shortest edge, we may as well sort which is O(n log n) where n is the number of edges * Next, we have n operations of constant time T(n) = O(n log n) + O(n) = O(n log n)
Travelling salesman problem Very famous problem: * Given a graph (V,E), what is the shortest path starting and ending at the same place that contains all vertices?
Brute Force Method There is no simple solution. Brute force method defines d(v,v’,X) to be the shortest distance from v to v’ using edges in X d (v,v) = 0 d (v,v’,X) = min { distance(e) + d(v’’,v’, X-{e}) | e is an edge in X from v to v’’}
Complexity of Solution Let n be the number of vertices in the graph. Then T(n) = n * T(n-1) So T is O(n!) which is VERY VERY bad!