Presentation on theme: "Lecture 15. Graph Algorithms"— Presentation transcript:
1Lecture 15. Graph Algorithms Talk at U of MarylandLecture 15. Graph AlgorithmsAn undirected graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite set of edges. An edge e ∈ E is an unordered pair (u,v), where u,v ∈ V.In a directed graph, the edge e is an ordered pair (u,v). An edge (u,v) is incident from vertex u and is incident to vertex v.A path from a vertex v to a vertex u is a sequence <v0,v1,v2,…,vk> of vertices where v0 = v, vk = u, and (vi, vi+1) ∈ E for i= 0, 1,…, k-1.The length of a path is defined as the number of edges in the path.
2a) An undirected graph and (b) a directed graph. Talk at U of MarylandExamplesa) An undirected graph and (b) a directed graph.
3Talk at U of MarylandMore DefinitionsAn undirected graph is connected if every pair of vertices is connected by a path.A forest is an acyclic graph, and a tree is a connected acyclic graph.A graph that has weights associated with each edge is called a weighted graph.
4Definitions and Representation Talk at U of MarylandDefinitions and RepresentationGraphs can be represented by their adjacency matrix or an edge (or vertex) list.Adjacency matrices have a value ai,j = 1 if nodes i and j share an edge; 0 otherwise. In case of a weighted graph, ai,j = wi,j, the weight of the edge.The adjacency list representation of a graph G = (V,E) consists of an array Adj[1..|V|] of lists. Each list Adj[v] is a list of all vertices adjacent to v.For a graph with n nodes, adjacency matrices take Θ(n2) space and adjacency list takes Θ(|E|) space.
5Definitions and Representation Talk at U of MarylandDefinitions and RepresentationAn undirected graph and its adjacency matrix representation.An undirected graph and its adjacency list representation.
6Talk at U of MarylandMinimum Spanning TreeA spanning tree of an undirected graph G is a subgraph of G that is a tree containing all the vertices of G.In a weighted graph, the weight of a subgraph is the sum of the weights of the edges in the subgraph.A minimum spanning tree (MST) for a weighted undirected graph is a spanning tree with minimum weight.
7An undirected graph and its minimum spanning tree. Talk at U of MarylandMinimum Spanning TreeAn undirected graph and its minimum spanning tree.
8Algorithms for computing MST Talk at U of MarylandAlgorithms for computing MSTPrim's algorithm and Kruskal's algorithm.Kruskal's algorithm is a greedy algorithm where, at each step, we choose the minimum-weight edge that doesn't create a cycle. We work with a forest. Initially, each vertex is in a tree by itself. We also work with a set of edges E', which initially holds all the edges of the graph. At each step, we remove and consider a minimum-weight edge from S (break ties arbitrarily). If it connects two different trees, we add it to the forest (joining two trees). Otherwise, it connects two edges in the same tree (forms a cycle), and we discard it.Prim's algorithm is also a greedy algorithm where, at each step, we choose a previously unconnected vertex that becomes connected by a lowest-weight edge. We choose an initial vertex arbitrarily and put it in a set V'. We also have a set E' of edges which is initially empty. At every step, we choose an edge (u,v) of minimal weight with u in V' and v not in V'. We then add v to V' and (u,v) to E'.
10Time complexity O(|E| log |E|) Talk at U of MarylandTime complexityO(|E| log |E|)Simple implementation of Union-Find gives you log n access time for each operation:Sets will be kept as linked lists of vertices. Each element in the list has a pointer to its next element, and a pointer to the head of the list. There is also a tail pointer to the end of the list, and the list maintains how many elements it contains. The "name" is the first vertex in the list. To do MAKE-SET, we create a linked list of a single element, and the num field set to 1. This costs O(1). To do a FIND, we follow the pointer to the head of the list and return the element there. This costs O(1). To do a UNION(u,v), we follow the pointer of u to its head, and the pointer of v to its head. There we find the number of elements in each list. If (say) u is in the smaller list, we link that list to the end of the bigger list (v's list). Then we traverse u's list, moving each head pointer to point to the head of v's list. Finally, we update the num field of v's list. This costs O(t), where t is the number of vertices of the smaller list.Because we always do the work on the smaller list, each UNION at least doubles the size of the smaller list. So an element's name gets updated at most O(log n) times. Thus n UNIONs take at most O(n log n) time.We will give more details in class.
11Talk at U of MarylandExampleAA433433B12B121174CDCD35685644EEF21F21Figure on the left shows a graph with sorted order (inside text boxes) written beside the edges plus the cost corresponding to each edge. Starting from A we can see A to C has order one so this edge will be added first to spanning treeSecond in order is an edge between E and F so we add it.Next we add edge between A and D and one edge between A and B. Edge between C and D is discarded as it creates a cycleNext an edge between D and F is added even if D and F have been explored. Edge between B and C is dropped and edge between C and E is dropped
12Minimum Spanning Tree: Prim's Algorithm Talk at U of MarylandMinimum Spanning Tree: Prim's Algorithm
13Time complexity O(|E| + |V| log |V|). Talk at U of MarylandTime complexityO(|E| + |V| log |V|).Using heap, gives you O(|E| log |V|). Since after decrease value, we need to re-do the heap.But this can be improved using Fibonacci heap (not covered here).
14Prim's minimum spanning tree algorithm. Talk at U of MarylandPrim's Algorithm ExamplePrim's minimum spanning tree algorithm.