Download presentation

Presentation is loading. Please wait.

1
**Lecture 15. Graph Algorithms**

Talk at U of Maryland Lecture 15. Graph Algorithms An 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.

2
**a) An undirected graph and (b) a directed graph.**

Talk at U of Maryland Examples a) An undirected graph and (b) a directed graph.

3
Talk at U of Maryland More Definitions An 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.

4
**Definitions and Representation**

Talk at U of Maryland Definitions and Representation Graphs 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.

5
**Definitions and Representation**

Talk at U of Maryland Definitions and Representation An undirected graph and its adjacency matrix representation. An undirected graph and its adjacency list representation.

6
Talk at U of Maryland Minimum Spanning Tree A 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.

7
**An undirected graph and its minimum spanning tree.**

Talk at U of Maryland Minimum Spanning Tree An undirected graph and its minimum spanning tree.

8
**Algorithms for computing MST**

Talk at U of Maryland Algorithms for computing MST Prim'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'.

9
Talk at U of Maryland Kruskal’s Algorithm

10
**Time complexity O(|E| log |E|)**

Talk at U of Maryland Time complexity O(|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.

11
Talk at U of Maryland Example A A 4 3 3 4 3 3 B 1 2 B 1 2 1 1 7 4 C D C D 3 5 6 8 5 6 4 4 E E F 2 1 F 2 1 Figure 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 tree Second 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 cycle Next 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

12
**Minimum Spanning Tree: Prim's Algorithm**

Talk at U of Maryland Minimum Spanning Tree: Prim's Algorithm

13
**Time complexity O(|E| + |V| log |V|).**

Talk at U of Maryland Time complexity O(|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).

14
**Prim's minimum spanning tree algorithm.**

Talk at U of Maryland Prim's Algorithm Example Prim's minimum spanning tree algorithm.

Similar presentations

OK

Graph Graph Types of Graphs Types of Graphs Data Structures to Store Graphs Data Structures to Store Graphs Graph Definitions Graph Definitions.

Graph Graph Types of Graphs Types of Graphs Data Structures to Store Graphs Data Structures to Store Graphs Graph Definitions Graph Definitions.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on shapes for kindergarten Ppt on digital energy meter Presentations ppt online training Full ppt on electron beam machining application Ppt online downloader for games Ppt on pf and esic Ppt on internet as a post office Free download ppt on conservation of plants and animals Ppt on male reproductive system of human Ppt on brand marketing manager