Download Graph Theory: Definitions, Minimum Spanning Tree Problem, and Algorithms - Prof. Jared C. and more Study notes Computer Science in PDF only on Docsity!
CS 561, Lecture 21
Jared Saia University of New Mexico
Today’s Outline
Graph Definition
- A graph is a pair of sets (V, E).
- We call V the vertices of the graph
- E is a set of vertex pairs which we call the edges of the graph.
- In an undirected graph, the edges are unordered pairs of vertices and in a directed graph, the edges are ordered pairs.
- We assume that there is never an edge from a vertex to itself (no self-loops) and that there is at most one edge from any vertex to any other (no multi-edges)
- |V | is the number of vertices in the graph and |E| is the number of edges
Graph Defns
- A graph G′^ = (V ′, E′) is a subgraph of G = (V, E) if V ′^ ⊆ V and E′^ ⊆ E
- If (u, v) is an edge in a graph, then u is a neighbor of v
- For a vertex v, the degree of v, deg(v), is equal to the number of neighbors of v
- A path is a sequence of edges, where each successive pair of edges shares a vertex and all edges are disjoint
- A graph is connected if there is a path from any vertex to any other vertex
- A disconnected graph consists of several connected compo- nents which are maximal connected subgraphs
- Two vertices are in the same component if and only if there is a path between them
Graph Defns
- A cycle is a path that starts and ends at the same vertex and has at least one edge
- A graph is acyclic if no subgraph is a cycle. Acyclic graphs are also called forests
- A tree is a connected acyclic graph. It’s also a connected component of a forest.
- A spanning tree of a graph G is a subgraph that is a tree and also contains every vertex of G. A graph can only have a spanning tree if it’s connected
- A spanning forest of G is a collection of spanning trees, one for each connected component of G
Minimum Spanning Tree Problem
- Suppose we are given a connected, undirected weighted graph
- That is a graph G = (V, E) together with a function w : E → R that assigns a weight w(e) to each edge e. (We assume the weights are real numbers)
- Our task is to find the minimum spanning tree of G, i.e., the spanning tree T minimizing the function
w(T ) =
∑ e∈T
w(e)
Example
Example
eighted graph and its minimum spanning tree
A weighted graph and its minimum spanning tree
Applications
- Creating an inexpensive road network to connect cities
- Wiring up homes for phone service with the smallest amount of wire
- Finding a good approximation to the TSP problem
Example
u v
e
e’
Proving that every safe edge is some minimum spanning tree.
- There are two major MST algorithm
- In Kruskal’s algorithm, the set A is
added to A is always a least-weighte
connects two distinct components
- In Prim’s algorithm, the set A form
edge added to A is always a least-w
the tree to a vertex not in the tree
Proof that every safe edge is in some MST. The red edges are the set A.
12
Corollary
Let G = (V, E) be a connected, undirected graph with a real- valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G, and let C = (Vc, Ec) be a connected component (tree) in the forest GA = (V, A). If (u, v) is a light edge connecting C to some other component in GA, then (u, v) is safe for A
Proof: The cut (VC, V − VC) respects A, and (u, v) is a light edge for this cut. Therefore (u, v) is safe for A.
Two MST algorithms
- There are two major MST algorithms, Kruskal’s and Prim’s
- In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted edge in the graph that connects two distinct components
- In Prim’s algorithm, the set A forms a single tree. The safe edge added to A is always a least-weighted edge connecting the tree to a vertex not in the tree
Kruskal’s Algorithm
- Q: In Kruskal’s algorithm, how do we determine whether or not an edge connects two distinct connected components?
- A: We need some way to keep track of the sets of vertices that are in each connected components and a way to take the union of these sets when adding a new edge to A merges two connected components
- What we need is the data structure for maintaining disjoint sets (aka Union-Find) that we discussed last week
Kruskal’s Algorithm
MST-Kruskal(G,w){ for (each vertex v in V) Make-Set(v); sort the edges of E into nondecreasing order by weight; for (each edge (u,v) in E taken in nondecreasing order){ if(Find-Set(u)!=Find-Set(v)){ A = A union (u,v); Set-Union(u,v); } } return A; }
Example Run
Kruskal’s Algorithm
- Q: In Kruskal’s algorithm, how do we determine whether or not an edge connects two distinct connected components?
- A: We need some way to keep track of the sets of vertices that are in each connected components and a way to take the union of these sets when adding a new edge to A merges two connected components
- What we need is the data structure for maintaining disjoint sets (aka Union-Find) that we discussed last week
Kruskal’s Algorithm
MST-Kruskal(G,w){ for (each vertex v in V) Make-Set(v); sort the edges of E into nondecreasing order by weight; for (each edge (u,v) in E taken in nondecreasing order){ if(Find-Set(u)!=Find-Set(v)){ A = A union (u,v); Set-Union(u,v); } } return A; }
Example Run
8 5 10 2 3 18 12 16 14
30
4 26
8 10 5 18 3 16 1214 30 4 26
8 10 5 18 12 16 4 14 3026
8 10 5 18 12 16 (^143026)
18
(^810) 12 16 (^143026)
10 12 16 (^143026) 12 16 (^143026)
16 18 18 (^143026) 3016 18 26
18
30 26
(^18 ) 26
30
Kruskal’s algorithm run on the example graph. Thick edges are in A. Dashed edges are useless.
Correctness?
- Correctness of Kruskal’s algorithm follows immediately from the corollary
- Each time we add the lightest weight edge that connects two connected components, hence this edge must be safe for A
- This implies that at the end of the algorith, A will be a MST
Kruskal’s algorithm run on the example graph. Thick edges are in A. Dashed edges are useless.
Correctness?
- Correctness of Kruskal’s algorithm follows immediately from the corollary
- Each time we add the lightest weight edge that connects two connected components, hence this edge must be safe for A
- This implies that at the end of the algorith, A will be a MST
Runtime?
- The runtime for Kruskal’s alg. will depend on the implemen- tation of the disjoint-set data structure. We’ll assume the im- plementation with union-by-rank and path-compression which we showed has amortized cost of log∗^ n
Prim’s Algorithm
- We can speed things up by noticing that the algorithm visits each vertex only once
- Rather than keeping the edges in the heap, we will keep a heap of vertices, where the key of each vertex v is the weight of the minimum-weight edge between v and A (or infinity if there is no such edge)
- Each time we add a new edge to A, we may need to decrease the key of some neighboring vertices
Prim’s
We will break up the algorithm into two parts, Prim-Init and Prim-Loop
Prim(V,E,s){ Prim-Init(V,E,s); Prim-Loop(V,E,s); }
Prim-Init
Prim-Init(V,E,s){ for each vertex v in V - {s}{ if ((v,s) is in E){ edge(v) = (v,s); key(v) = w((v,s)); }else{ edge(v) = NULL; key(v) = infinity; } } Heap-Insert(v); }
Prim-Loop
Prim-Loop(V,E,s){ A = {}; for (i = 1 to |V| - 1){ v = Heap-ExtractMin(); add edge(v) to A; for (each edge (u,v) in E){ if (u is not in A AND key(u) > w(u,v)){ edge(u) = (u,v); Heap-DecreaseKey(u,w(u,v)); } } } return A; }
Runtime?
- The runtime of Prim’s is dominated by the cost of the heap operations Insert, ExtractMin and DecreaseKey
- Insert and ExtractMin are each called O(|V |) times
- DecreaseKey is called O(|E|) times, at most twice for each edge
- If we use a Fibonacci Heap, the amortized costs of Insert and DecreaseKey is O(1) and the amortized cost of ExtractMin is O(log |V |)
- Thus the overall run time of Prim’s is O(|E| + |V | log |V |)
- This is faster than Kruskal’s unless E = O(|V |)
Note
- This analysis assumes that it is fast to find all the edges that are incident to a given vertex
- We have not yet discussed how we can do this
- This brings us to a discussion of how to represent a graph in a computer
Graph Representation
There are two common data structures used to explicity repre- sent graphs
- Adjacency Matrices
- Adjacency Lists
Adjacency Matrix
- The adjacency matrix of a graph G is a |V | × |V | matrix of 0’s and 1’s
- For an adjacency matrix A, the entry A[i, j] is 1 if (i, j) ∈ E and 0 otherwise
- For undirectd graphs, the adjacency matrix is always sym- metric: A[i, j] = A[j, i]. Also the diagonal elements A[i, i] are all zeros
Adjacency Lists
- The total space required for an adjacency list is O(|V | + |E|)
- Listing all the neighbors of a node v takes O(1 + deg(v)) time
- We can determine if (u, v) is an edge in O(1 + deg(u)) time by scanning the neighbor list of u
- Note that we can speed things up by storing the neighbors of a node not in lists but rather in hash tables
- Then we can determine if an edge is in the graph in expected O(1) time and still list all the neighbors of a node v in O(1 + deg(v)) time