Note Taker: Bhargavi Latha Ballem

Lecture # 5

Date: Sept 16th 1998. 

GRAPHS

 

Definitions :

Graph: A graph G = (V, E) consists of a finite, nonempty set of vertices V and a set of edges of E. If the edges are ordered pairs (u, v) of vertices, then the graph is said to be directed graph , u is called the tail and v is called the head of the edge (u, v). If the edges are unordered pairs (sets) of distinct vertices, also denoted by (u, v), then the graph is said to be undirected graph. If there are weights associated with the edges then the graph is a weighted graph.

Adjacent: If (u, v) is an edge in a graph G = (V , E) The vertex v is adjacent to vertex u. When the graph is undirected the adjacency relation is symmetric. When the graph is directed the relation is not necessarily symmetric.

Degree of a vertex: It is the number of vertices adjacent to a given vertex.

Path: Path is a sequence of vertices such that every pair of consecutive vertices are adjacent. The length of the path is the number edges in the path. A path is simple if all vertices in the path are distinct. 

Cycle: In a graph G, a path ( V0,V1,……………,Vk ) forms a cycle if V0 = Vk and the path contains at least one edge.

Subgraph : A graph G˘ = (V˘ , E˘ ) is a subgraph of G (V , E ) if V˘ Í V and E˘ Í E. 

Induced subgraph: Given a set V˘ Í V, the subgraph of G is induced by V˘ denoted by G[ V˘ ] is the graph G˘ = (V˘ , E˘ ) , where E˘ = { (u, v) Î E : u, v Î V˘ }.

Connected Graph: A graph is connected if every pair of vertices is connected by a path.

Complement Graph: For a graph G the complement G =(V, ` E) where E = { (u, v) : (u, v) Ï E } i.e. ` G is the graph containing exactly those edges that are not in G.

Tree: A connected acyclic graph is a tree and a group of trees is a Forest.

Spanning tree: A spanning tree of a graph is a subgraph that contains all the vertices of the graph and is a tree. A graph may have many spanning trees. A graph is not connected has no spanning tree.

Minimum spanning tree: This is defined only for weighted connected graphs. The total weight of a tree is the sum of the weights of its edges .The minimum spanning tree is a spanning tree with the minimum total weight. There can be more than one minimum spanning tree for each graph.

Representation:

There are two standard ways of representing a graph they are 

Adjacency Lists:

This representation is preferred because it provides a compact way to represent sparse graphs. The graph G = (V, E) consists of an array Adj of | V | lists, one for each vertex in V. For each u in V, the adjacency list Adj[u] consists of all the vertices adjacent to u in G. Let |V| = n and |E| = m

Whether the graph is directed or not the amount of memory an adjacency list requires is O( max(V, E ) ) = O ( n + m ).

A disadvantage of this representation is that there is no faster way to determine if a given edge (u, v) is present in the graph than to search for v in the adjacency list Adj[u].  

Adjacency Matrix:

For a graph G (V, E ) , we assume that vertices are numbered 1,2,……, | V | in some arbitrary manner. The adjacency matrix representation of a graph G consists of a

| V | ´ | V | matrix A =( aij ) such that

 

1 if ( i, j ) Î E,

aij =

0 otherwise.

The adjacency matrix requires O (n 2) memory independent of the number edges in the graph. The Simplicity of this representation may make it preferable when graphs are reasonably small or when the graph is dense. Moreover when the graph is unweighted there is an additional advantage in store of adjacency matrix representation. Rather than using one word of computer memory for each matrix entry, the adjacency matrix uses only one bit per entry.

 

Minimum Spanning Trees :

Two algorithms:

Kruskal's Algorithm:

Prim's Algorithm:

  1. Both are greedy, meaning they build the tree step by step, at each step selecting the lowest weight edge from the remaining unselected edges.
  2. The algorithm's manages a set A that is always a subset of some minimum spanning tree. At each step an edge (u, v) is determined that can be added to A without violating this invariant which means A È { (u, v) } is also a subset of the MST. Such an edge is called a safe edge for A.

3. Both end up producing a spanning tree of minimum cost.

 Input : An undirected graph G = (V , E) with a weight function w on the edges. The graph has n vertices and m edges

Output : A minimum-cost spanning tree for graph G.

 Kruskal’s Algorithm: 

Initially, all of the edges are removed from the graph and sorted in non-decreasing order, thus creating disjoint sets of V, one for each vertex containing only that vertex. If an edge connects two vertices in disjoint subsets, the edges added and the subsets are merged into one set. Thus every added edge reduces the number of disjoint sets by one. This process is repeated until the graph is a tree.

Refer to the algorithm on pg 505 of [CLR]

 Analysis of Kruskal's Algorithm:

The naive method takes O(m n).Step 1 sorting takes O(m log n) and Step 2 checks for a cycle that takes O(m n) time complexity.

The analysis for our algorithm takes into account the time taken for the operations Make_ Set, Sorting, Find_ Set, Union. Steps 1-3 initialize the set A to the empty set and create |V | trees one containing each vertex. Step 1 takes constant amount of time. Step 2 and 3 run in O(n) time complexity. Step 4 runs in O (m log m) times the edges are sorted in order by non -decreasing weights. The for loop in step 5 runs in O( m ) ´ O(log m) time. Steps 6 and 7 run in O(m log n ) and Step 8 i.e. Union runs in constant amount of time.

 Kruskal’s algorithm runs in T(n) = O(m log m) = O( m log n),which is much better than the naïve algorithm.

Prim's Algorithm :

The interesting thing about Prim's algorithm is that the edges in the growing set A always form a single (i.e. connected) tree. At each step, you add one more edge to this tree (a light edge that goes from A to V - A), and this tree grows till you span V. The nodes in V - A are maintained in a priority queue sorted by the weight of the minimum weight edge that connects the node to a node in A. The running time of the algorithm depends on whether the priority queue is implemented as an unsorted array, a binary heap, or a Fibonacci heap.

Refer to the algorithm on pg 509 of [CLR]

 Analysis of Prim's Algorithm:

The performance of Prim's algorithm depends on how the priority queue Q is implemented. Essentially, the EXTRACT-MIN operation gets executed n times, and the DECREASE-KEY operation gets executed O(m) times. The priority queue can be implemented as an unsorted array, a binary heap, or a Fibonacci heap. Each of these gives different running times of the above operations.

Step 1 runs O(n) time for initialization of the priority queue with all the vertices of the graph .Steps 2 and 3 also run in O(n) here the key value is set according to the weight associated to the vertices adjacent to the starting vertex. Steps 6 and 7 run in O(n log n) here the key with the minimum value is selected .Step 8 runs in O(n) here it adds vertex u to the tree from the priority queue. Steps 9 and 10 run in constant amount of time. Step 10 updates the parent of v to u. Step 11 updates the key value and it runs in O(log n ) time.

 Prim’s Algorithm runs in T(n) = O(m+(n log n)). This can be improved to O((m+ n ) log n ) using Fibonacci heaps for the priority Queue implementation.

 

Proof Of Correctness :

 Theorem: Refer to page 501 of [CLR]

Let G =(V, E) be connected, undirected graph With a real value weight function w defined on E. Let A be a subset of E That is included in some MST for G, let (S, V-S) be a partition of V[G] such that no edge of A crosses the partition, and let (u, v) be the lightest edge crossing in E crossing the partition. Then, A È {(u, v)} is also included in some MST for G.

The following are some observations we will need to prove the above theorem.

Property 1: There is a unique path between every pair of vertices in a tree T.

Property 2: If an edge (u, v) is added to a spanning tree T, it forms precisely one cycle with the path connecting u and v in T.

Property 3: Every spanning tree of a connected undirected graph has precisely n –1 edges.

Before we Prove the above theorem we show how to prove the correctness of both Kruskal’s and Prim’s algorithms by using the above theorem. Both the algorithms can be shown using induction. Proof by induction specifies a base case and then we prove that it is correct for n-1 terms and by induction we show that it applies for n terms also.

In Kruskal’s algorithm the base case is that set A is a forest. The safe edge that is added to A is always the least weight edge that joins two distinct components. This algorithm finds a safe edge to add to the growing forest by finding all the edges that connect any two trees in the forest, an edge (u, v) which forms a MST. Since (u, v) is the edge with least weight this proves by induction that we have a minimum spanning tree.

In Prim’s algorithm the base case is very simple since we choose the vertex with the least weight associated with it .The set A here forms a single tree. The next edge added to the tree is always an edge with the least weight thus forming a MST .Thus by induction Prim’s algorithm always forms a minimum spanning tree.

Proof:

We prove this Theorem using contradiction.

Let ‘T’ be a MST that includes A and assume that A È {u, v} is not a part of the MST. Now add the edge (u, v) into T this minimum spanning tree. From property 2 we see that it forms a cycle. This implies that there are some edges that cross the partition and connect two sets S, V-S. The edge with the least edge say (x, y) is found from the existing cycle and replaced by the edge (u, v). This forms a MST with even lesser weight.

Contradicting the assumption that was made earlier.

This proves that both Kruskal’s and Prim’s algorithms give a minimum spanning tree for a graph G.

 

Some applications of minimum spanning trees :

Phone network design

Computer networks

Traveling Salesman Problem