0% found this document useful (0 votes)
32 views

Unit V

Uploaded by

Juned Pathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Unit V

Uploaded by

Juned Pathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

UNIT-V

Graphs
Graph and Graph Models, Graph terminology and special types of graphs

A graph is a mathematical object that is used to model different situations – objects and
processes:
1. Linked list
2. Tree
3. Flowchart of a program
4. Structure chart of a program
5. Finite state automata
6. City map
7. Electric circuits
8. Course curriculum
Definition
A graph is a collection (nonempty set) of vertices and edges

Vertices: can have names and properties

Edges: connect two vertices, can be labeled, can be directed

Adjacent vertices: if there is an edge between them.

Example:
Vertices: A,B,C,D
Edges: AB, AC, BC, CD
Graph1:

Same graph given in another way:


Directed graphs and undirected graphs
There are two basic types of graphs - directed and undirected.
In undirected graphs the edges are symmetrical, e.g. if A and B are vertices,
A B and B A are one and the same edge.
Graph1 above is undirected.
In directed graphs the edges are oriented, they have a beginning and an end.
Thus A B and B A are different edges.
Sometimes the edges of a directed graph are called arcs.
Examples of directed graphs
Graph2: Graph3:

Graph2 and Graph3 are different graphs


Some definitions of basic graph concepts differ slightly depending on whether we talk about
directed or undirected graphs.
Paths
A path is a list of vertices in which successive vertices are connected by edges
Examples
Some paths in Graph1 :
ABCD
ACBACD
AB
DCB
CBA
Some paths in Graph2:
DAB
ADA
C
Note that D A B is a path in Graph3, however A D A C is not a path in Graph3because A C is
not an edge (the edge is C A).
Note: In different textbooks you may find different definitions for a path in a graph. Apart from
the usual confusion, there is nothing wrong in this as long as the definitions are followed
throughout the presented theory.
Simple path No vertex is repeated.
Examples:
In Graph1, D C B A is a simple path, while D C B A C is not a simple path
In Graph2, D A B is a simple path, while D A D B is not a simple path
Cycles
A cycle is a simple path with distinct edges, where the first vertex is equal to the last.

Examples:
Cycles in Graph1: C A B C, C B A C, A B C A, A C B A, B A C B, B C A B
A B A is not a cycle, because the edge A B is the same as B
A Cycles in Graph3: A D A, D A B D
A graph without cycles is called acyclic graph
Loop
An edge that connects the vertex with itself

Connected graphs
Connected graph: There is a path between each two vertices
Graph1, Graph2 and Graph3 are connected graphs.
Disconnected graph: There are at least two vertices not connected by a path.
Examples of disconnected graphs:
Graph4 Graph5
Vertices: A,B,C,D Vertices: A,B,C,D
Edges: AB, CD Edges: AB, AC
Isomorphic graphs
Two graphs which contain the same number of graph vertices connected in the same way are said
to be isomorphic. Formally, two graphs and with graph vertices are said to
be isomorphic if there is a permutation of such that is in the set of graph edges
iff is in the set of graph edges .
Two graphs G1 and G2 are said to be isomorphic if −
 Their number of components (vertices and edges) are same.
 Their edge connectivity is retained.
Note − In short, out of the two isomorphic graphs, one is a tweaked version of the other. An
unlabelled graph also can be thought of as an isomorphic graph.
There exists a function ‘f’ from vertices of G1 to vertices of G2
[f: V(G1) ⇒ V(G2)], such that
Case (i): f is a bijection (both one-one and onto)
Case (ii): f preserves adjacency of vertices, i.e., if the edge {U, V} ∈ G1, then the edge {f(U),
f(V)} ∈ G2, then G1 ≡ G2.
Note
If G1 ≡ G2 then −
 |V(G1)| = |V(G2)|
 |E(G1)| = |E(G2)|
 Degree sequences of G1 and G2 are same.
 If the vertices {V1, V2, .. Vk} form a cycle of length K in G1, then the vertices {f(V1),
f(V2),… f(Vk)} should form a cycle of length K in G2.
All the above conditions are necessary for the graphs G1 and G2 to be isomorphic, but not
sufficient to prove that the graphs are isomorphic.
 (G1 ≡ G2) if and only if (G1− ≡ G2−) where G1 and G2 are simple graphs.
 (G1 ≡ G2) if the adjacency matrices of G1 and G2 are same.
 (G1 ≡ G2) if and only if the corresponding subgraphs of G1 and G2(obtained by deleting
some vertices in G1 and their images in graph G2) are isomorphic.
Example
Which of the following graphs are isomorphic?

In the graph G3, vertex ‘w’ has only degree 3, whereas all the other graph vertices has degree 2.
Hence G3 not isomorphic to G1 or G2.
Taking complements of G1 and G2, you have −
Here, (G1− ≡ G2−), hence (G1 ≡ G2).

Connectivity

Euler graph
A closed walk in a graph G containing all the edges of G is called an Euler line in G. A graph
containing an Euler line is called an Euler graph. We know that a walk is always connected.
Since the Euler line (which is a walk) contains all the edges of the graph, an Euler graph is
connected except for any isolated vertices the graph may contain. As isolated vertices do not
contribute anything to the understanding of an Euler graph, it is assumed now onwards that Euler
graphs do not have any isolated vertices and are thus connected.
Example Consider the graph shown in Figure. Clearly, v1 e1 v2 e2 v3 e3 v4 e4 v5 e5 v3 v6 e7 v1
in (a) is an Euler line, whereas the graph shown in (b) is non-Eulerian.

Eulerian graph Non- Eulerian graph


Eulerian Path is a path in graph that visits every edge exactly once. Eulerian Circuit is an
Eulerian Path which starts and ends on the same vertex.
How to find whether a given graph is Eulerian or not? The problem is same as following
question. “Is it possible to draw a given graph without lifting pencil from the paper and without
tracing any of the edges more than once”.
A graph is called Eulerian if it has an Eulerian Cycle and called Semi-Eulerian if it has an
Eulerian Path. The problem seems similar to Hamiltonian Path which is NP complete problem
for a general graph. Fortunately, we can find whether a given graph has a Eulerian Path or not in
polynomial time. In fact, we can find it in O(V+E) time.
Following are some interesting properties of undirected graphs with an Eulerian path and cycle.
We can use these properties to find whether a graph is Eulerian or not.
Eulerian Cycle
An undirected graph has Eulerian cycle if following two conditions are true.
….a) All vertices with non-zero degree are connected. We don’t care about vertices with zero
degree because they don’t belong to Eulerian Cycle or Path (we only consider all edges).
….b) All vertices have even degree.
Eulerian Path
An undirected graph has Eulerian Path if following two conditions are true.
….a) Same as condition (a)for Eulerian Cycle….b) If zero or two vertices have odd degree and
all other vertices have even degree. Note that only one vertex with odd degree is not possible in an
undirected graph (sum of all degrees is always even in an undirected graph)
Note that a graph with no edges is considered Eulerian because there are no edges to traverse.
How does this work? In Eulerian path, each time we visit a vertex v, we walk through two
unvisited edges with one end point as v. Therefore, all middle vertices in Eulerian Path must
have even degree. For Eulerian Cycle, any vertex can be middle vertex, therefore all vertices
must have even degree.

Hamiltonian graphs
A cycle passing through all the vertices of a graph is called a Hamiltonian cycle. A graph
containing a Hamiltonian cycle is called a Hamiltonian graph. A path passing through all the
vertices of a graph is called a Hamiltonian path and a graph containing a Hamiltonian path is said
to be traceable. Examples of Hamiltonian graphs are given in Figure.

If the last edge of a Hamiltonian cycle is dropped, we get a Hamiltonian path. However, a non-
Hamiltonian graph can have a Hamiltonian path, that is, Hamiltonian paths cannot always be
used to form Hamiltonian cycles. For example, in Figure, G1 has no Hamiltonian path, and so no
Hamiltonian cycle; G2 has the Hamiltonian path v1v2v3v4, but has no Hamiltonian cycle, while
G3 has the Hamiltonian cycle v1v2v3v4v1.

A multigraph or general graph is Hamiltonian if and only if its underlying graph is Hamiltonian,
because if G is Hamiltonian, then any Hamiltonian cycle in G remains a Hamiltonian cycle in the
underlying graph of G. Conversely, if the underlying graph of a graph G is Hamiltonian, then G
is also Hamiltonian.
Let G be a graph with n vertices. Clearly, G is a subgraph of the complete graph Kn. From G, we
construct step by step supergraphs of G to get Kn, by adding an edge at each step between two
vertices that are not already adjacent.

Now, let us start with a graph G which is not Hamiltonian. Since the final outcome of the
procedure is the Hamiltonian graph Kn, we change from a non-Hamiltonian graph to a
Hamiltonian graph at some stage of the procedure. For example, the non-Hamiltonian graph G1
above is followed by the Hamiltonian graph G2. Since supergraphs of Hamiltonian graphs are
Hamiltonian, once a Hamiltonian graph is reached in the procedure, all the subsequent
supergraphs are Hamiltonian.

Planar graph

A planar graph is an undirected graph that can be drawn on a plane without any edges crossing.
Such a drawing is called a planar representation of the graph in the plane. Ex : K4 is a planar
graph

Other planar representations of K4

Q3 is a planar graph
K1,n and K2,n are planar graphs for all n

Euler’s Planar Formula


Definition : A planar representation of a graph splits the plane into regions, where one of them
has infinite area and is called the infinite region.

Ex : 2 regions
Euler’s Planar Formula
Let G be a connected planar graph, and consider a planar representation of G.
Let V = # vertices, E = # edges, F = # regions.
Theorem : V + F = E + 2.

Graph Coloring
Graph coloring is the procedure of assignment of colors to each vertex of a graph G such that no
adjacent vertices get same color. The objective is to minimize the number of colors while
coloring a graph. The smallest number of colors required to color a graph G is called its
chromatic number of that graph. Graph coloring problem is a NP Complete problem.
Method to Color a Graph
The steps required to color a graph G with n number of vertices are as follows −
Step 1 − Arrange the vertices of the graph in some order.
Step 2 − Choose the first vertex and color it with the first color.
Step 3 − Choose the next vertex and color it with the lowest numbered color that has not been
colored on any vertices adjacent to it. If all the adjacent vertices are colored with this color,
assign a new color to it. Repeat this step until all the vertices are colored.
Example

In the above figure, at first vertex aa is colored red. As the adjacent vertices of vertex a are again
adjacent, vertex bb and vertex dd are colored with different color, green and blue respectively.
Then vertex cc is colored as red as no adjacent vertex of cc is colored red. Hence, we could color
the graph by 3 colors. Hence, the chromatic number of the graph is 3.
Applications of Graph Coloring
Some applications of graph coloring include −
 Register Allocation

 Map Coloring

 Bipartite Graph Checking

 Mobile Radio Frequency Assignment

 Making time table, etc.

Shortest path problems


Graph Traversal
Graph traversal is the problem of visiting all the vertices of a graph in some systematic order.
There are mainly two ways to traverse a graph.
 Breadth First Search
 Depth First Search
Breadth First Search
Breadth First Search (BFS) starts at starting level-0 vertex XX of the graph GG. Then we visit all
the vertices that are the neighbors of XX. After visiting, we mark the vertices as "visited," and
place them into level-1. Then we start from the level-1 vertices and apply the same method on
every level-1 vertex and so on. The BFS traversal terminates when every vertex of the graph has
been visited.

BFS Algorithm
The concept is to visit all the neighbor vertices before visiting other neighbor vertices of
neighbor vertices.
 Initialize status of all nodes as “Ready”.

 Put source vertex in a queue and change its status to “Waiting”.


 Repeat the following two steps until queue is empty −

o Remove the first vertex from the queue and mark it as “Visited”.

o Add to the rear of queue all neighbors of the removed vertex whose status is
“Ready”. Mark their status as “Waiting”.

Problem
Let us take a graph (Source vertex is ‘a’) and apply the BFS algorithm to find out the traversal
order.

Solution −
 Initialize status of all vertices to “Ready”.

 Put a in queue and change its status to “Waiting”.

 Remove a from queue, mark it as “Visited”.

 Add a’s neighbors in “Ready” state b, d and e to end of queue and mark them as
“Waiting”.

 Remove b from queue, mark it as “Visited”, put its “Ready” neighbor cat end of queue
and mark c as “Waiting”.

 Remove d from queue and mark it as “Visited”. It has no neighbor in “Ready” state.

 Remove e from queue and mark it as “Visited”. It has no neighbor in “Ready” state.

 Remove c from queue and mark it as “Visited”. It has no neighbor in “Ready” state.

 Queue is empty so stop.

So the traversal order is −


a→b→d→e→ca→b→d→e→c
The alternate orders of traversal are −
a→b→e→d→ca→b→e→d→c
Or, a→d→b→e→ca→d→b→e→c
Or, a→e→b→d→ca→e→b→d→c
Or, a→b→e→d→ca→b→e→d→c
Or, a→d→e→b→ca→d→e→b→c
Application of BFS
 Finding the shortest path

 Minimum spanning tree for un-weighted graph

 GPS navigation system

 Detecting cycles in an undirected graph

 Finding all nodes within one connected component

Complexity Analysis
Let G(V,E)G(V,E) be a graph with |V||V| number of vertices and |E||E| number of edges. If
breadth first search algorithm visits every vertex in the graph and checks every edge, then its
time complexity would be −
O(|V|+|E|).O(|E|)O(|V|+|E|).O(|E|)
It may vary between O(1)O(1) and O(|V2|)O(|V2|)
Depth First Search
Depth First Search (DFS) algorithm starts from a vertex vv, then it traverses to its adjacent vertex
(say x) that has not been visited before and marks as "visited" and goes on with the adjacent
vertex of xx and so on.
If at any vertex, it encounters that all the adjacent vertices are visited, then it backtracks until it
finds the first vertex having an adjacent vertex that has not been traversed before. Then, it
traverses that vertex, continues with its adjacent vertices until it traverses all visited vertices and
has to backtrack again. In this way, it will traverse all the vertices reachable from the initial
vertex vv.
DFS Algorithm
The concept is to visit all the neighbor vertices of a neighbor vertex before visiting the other
neighbor vertices.
 Initialize status of all nodes as “Ready”

 Put source vertex in a stack and change its status to “Waiting”

 Repeat the following two steps until stack is empty −

o Pop the top vertex from the stack and mark it as “Visited”

o Push onto the top of the stack all neighbors of the removed vertex whose status is
“Ready”. Mark their status as “Waiting”.

Problem
Let us take a graph (Source vertex is ‘a’) and apply the DFS algorithm to find out the traversal
order.
Solution
 Initialize status of all vertices to “Ready”.

 Push a in stack and change its status to “Waiting”.

 Pop a and mark it as “Visited”.

 Push a’s neighbors in “Ready” state e, d and b to top of stack and mark them as
“Waiting”.

 Pop b from stack, mark it as “Visited”, push its “Ready” neighbor conto stack.

 Pop c from stack and mark it as “Visited”. It has no “Ready” neighbor.

 Pop d from stack and mark it as “Visited”. It has no “Ready” neighbor.

 Pop e from stack and mark it as “Visited”. It has no “Ready” neighbor.

 Stack is empty. So stop.

So the traversal order is −


a→b→c→d→ea→b→c→d→e
The alternate orders of traversal are −
a→e→b→c→da→e→b→c→d
Or, a→b→e→c→da→b→e→c→d
Or, a→d→e→b→ca→d→e→b→c
Or, a→d→c→e→ba→d→c→e→b
Or, a→d→c→b→ea→d→c→b→e
Complexity Analysis
Let G(V,E)G(V,E) be a graph with |V||V| number of vertices and |E||E| number of edges. If DFS
algorithm visits every vertex in the graph and checks every edge, then the time complexity is −
⊝(|V|+|E|)⊝(|V|+|E|)
Applications
 Detecting cycle in a graph
 To find topological sorting
 To test if a graph is bipartite
 Finding connected components
 Finding the bridges of a graph
 Finding bi-connectivity in graphs
 Solving the Knight’s Tour problem
 Solving puzzles with only one solution

Digraphs
A graph in which each graph edge is replaced by a directed graph edge, also called a digraph. A
directed graph having no multiple edges or loops (corresponding to a binary adjacency
matrix with 0s on the diagonal) is called a simple directed graph. A complete graph in which
each edge is bidirected is called a complete directed graph. A directed graph having no
symmetric pair of directed edges (i.e., no bidirected edges) is called an oriented graph. A
complete oriented graph (i.e., a directed graph in which each pair of nodes is joined by a single
edge having a unique direction) is called a tournament.
If is an undirected connected graph, then one can always direct the circuit graph
edges of and leave the separating edges undirected so that there is a directed path from any
node to another. Such a graph is said to be transitive if the adjacency relation is transitive.
When drawing a directed graph, the edges are typically drawn as arrows indicating the direction,
as illustrated in the following figure.

A directed graph with 10 vertices (or nodes) and 13 edges


One can formally define a directed graph as G=(N,E)G=(N,E), consisting of the set NN of nodes
and the set EE of edges, which are ordered pairs of elements of NN.

Directed acyclic graphs


Directed acyclic graphs (DAGs) are used to model probabilities, connectivity, and causality. A
“graph” in this sense means a structure made from nodes and edges.
 Nodes are usually denoted by circles or ovals (although technically they can be any shape
of your choosing).

 Edges are the connections between the nodes. An edge connects two nodes. They are
usually represented by lines, or lines with arrows.

DAGs are based on basic acyclic graphs.

A tree with nodes A B C D E F and G.


An acyclic graph is a graph without cycles (a cycle is a complete circuit). When following the
graph from node to node, you will never visit the same node twice.

This graph (the thick black line) is acyclic, as it has no cycles (complete circuits).
A connected acyclic graph, like the one above, is called a tree. If one or more of the tree
“branches” is disconnected, the acyclic graph is a called a forest.

This graph has a complete circuit and so is not acyclic.

A directed acyclic graph is an acyclic graph that has a direction as well as a lack of cycles.

The parts of the above graph are:


 Integer = the set for the the Vertices.

 Vertices set = {1,2,3,4,5,6,7}.

 Edge set = {(1,2), (1,3), (2,4), (2,5), (3,6), (4,7), (5,7), (6,7)}.

A directed acyclic graph has a topological ordering. This means that the nodes are ordered so
that the starting node has a lower value than the ending node. A DAG has a unique topological
ordering if it has a directed path containing all the nodes; in this case the ordering is the same as
the order in which the nodes appear in the path.
In computer science, DAGs are also called wait-for-graphs. When a DAG is used to detect a
deadlock, it illustrates that a resources has to wait for another process to continue.

Weighted digraphs
We can assign numbers to the edges or vertices of a graph in order to enable them to be used in
physical problems. Such an assignment is called the weight of the edges or vertices.
Weighted graphs are defined as the quadruples (V, E, f, g) or the triplets (V, E, f)or the triplets (V,
E, g), where V is the set of vertices, E is the set of domains, f is the function with domain V,
which assigns weights to vertices and g is the function with domain E, which assigns weights to
edges
Example
Following diagram is a weighted digraph which represents the communication network among
five individuals v1,v2,v3,v4,v5. The number assigned for each directed edge gives the
probability of their communication.

Planarity – “A graph is said to be planar if it can be drawn on a plane without any edges
crossing. Such a drawing is called a planar representation of the graph.”
Important Note – A graph may be planar even if it is drawn with crossings, because it may be
possible to draw it in a different way without crossings.
For example consider the complete graph k4 and its two possible planar representations –

 Example – Is the hypercube Q3 planar?


 Solution – Yes, Q3 is planar. Its planar representation-

Regions in Planar Graphs –


The planar representation of a graph splits the plane into regions. These regions are bounded by
the edges except for one region that is unbounded. For example, consider the following graph ”

There are a total of 6 regions with 5 bounded regions and 1 bounded region R6.
All the planar representations of a graph split the plane in the same number of regions. Euler
found out the number of regions in a planar graph as a function of the number of vertices and
number of edges in the graph.
Theorem – “Let G be a connected simple planar graph with e edges and v vertices. Then the
number of regions r in the graph is equal to e-v+2.”
 Example – What is the number of regions in a connected planar simple graph with 20
vertices each with a degree of 3?

 Solution – Sum of degrees of edges = 20 * 3 = 60. By handshaking theorem,


2e=60 which gives e=30 .
By Euler’s theorem, the number of regions = e-v+2 which gives 12 regions.

An important result obtained by Euler’s formula is the following inequality –


Note –“If G is a connected planar graph with e edges and v vertices, where v>=3, then e<=ev-6.
Also G cannot have a vertex of degree exceeding 5.”
 Example – Is the graph K5 planar?

 Solution – Number of vertices and edges in K5 is 5 and 10 respectively. Since 10 > 3*5 –
6, 10 > 9 the inequality e<=3v-6 is not satisfied. Thus the graph is not planar.

In graph theory, graph coloring is a special case of graph labeling; it is an assignment of labels
traditionally called "colors" to elements of a graph subject to certain constraints. In its simplest
form, it is a way of coloring the vertices of a graph such that no two adjacent vertices share the
same color; this is called a vertex coloring. Similarly, an edge coloring assigns a color to each
edge so that no two adjacent edges share the same color, and a face coloring of a planar graph
assigns a color to each face or region so that no two faces that share a boundary have the same
color.

Vertex coloring is the starting point of the subject, and other coloring problems can be
transformed into a vertex version. For example, an edge coloring of a graph is just a vertex
coloring of its line graph, and a face coloring of a planar graph is just a vertex coloring of its
planar dual. However, non-vertex coloring problems are often stated and studied as is. That is
partly for perspective, and partly because some problems are best studied in non-vertex form, as
for instance is edge coloring.
The convention of using colors originates from coloring the countries of a map, where each face
is literally colored. This was generalized to coloring the faces of a graph embedded in the plane.
By planar duality it became coloring the vertices, and in this form it generalizes to all graphs. In
mathematical and computer representations it is typical to use the first few positive or
nonnegative integers as the "colors". In general one can use any finite set as the "color set". The
nature of the coloring problem depends on the number of colors but not on what they are.

Graph coloring enjoys many practical applications as well as theoretical challenges. Beside the
classical types of problems, different limitations can also be set on the graph, or on the way a
color is assigned, or even on the color itself. It has even reached popularity with the general
public in the form of the popular number puzzle Sudoku. Graph coloring is still a very active
field of research.
A proper vertex coloring of the Petersen graph with 3 colors, the
minimum number possible

Vertex coloring
When used without any qualification, a coloring of a graph is almost always a proper vertex
coloring, namely a labelling of the graph’s vertices with colors such that no two vertices sharing
the same edge have the same color. Since a vertex with a loop could never be properly colored, it
is understood that graphs in this context are loopless.
The terminology of using colors for vertex labels goes back to map coloring. Labels like red and
blue are only used when the number of colors is small, and normally it is understood that the
labels are drawn from the integers {1,2,3,...}.
A coloring using at most k colors is called a (proper) k-coloring. The smallest number of colors
needed to color a graph G is called its chromatic number, χ(G). A graph that can be assigned a
(proper) k-coloring is k-colorable, and it is k-chromatic if its chromatic number is exactly k. A
subset of vertices assigned to the same color is called a color class, every such class forms an
independent set. Thus, a k-coloring is the same as a partition of the vertex set into k independent
sets, and the terms k-partite and k-colorable have the same meaning.

This graph can be 3-colored in 12 different ways.

The following table gives the chromatic number for familiar classes of graphs.

Trees and Applications of Trees


A tree is an undirected graph with no cycles and a vertex chosen
to be the root of the tree.
Note: in a tree, when we choose a root we impose an orientation.
Given an acyclic graph, we may choose any node to be the root of a tree.
Example
Acyclic graph:

If we choose vertex a to be the root, then we get the following tree:

If we choose c to be the root, then the tree will be:

Note also, that the graph does not specify the order of the children of a given node.
A spanning tree of a graph
A spanning tree of an undirected graph is a subgraph that contains all the vertices, and no cycles.
If we add any edge to the spanning tree, it forms a cycle, and the tree becomes a graph.
It is possible to define a spanning tree for directed graphs, however the definition is rather
complicated and will not be discussed here.
Example:
Spanning trees for Graph1:

A tree with N vertices has N-1 edges.


A graph with less than N-1 edges is not connected.
Complete graphs
Graphs with all edges present – each vertex is connected to all other vertices,
are called complete graphs.
Example:

Dense graphs: relatively few of the possible edges are missing


Sparse graphs: relatively few of the possible edges are present
Weighted graphs
Weights are assigned to each edge (e.g. distances in a road map)
Networks - directed weighted graphs
Note: Some textbooks define networks to be undirected weighted graphs as well.
Examples:
Graph representation
a. Adjacency matrix

Vertices: A,B,C,D
Edges: AB, AC, BD,
CD
A B C D
A 0 1 1 0
B 1 0 0 1
C 1 0 0 1
D 0 1 1 0
Note: Depending on the application sometimes the diagonal is set to 1
For not directed graphs the matrix is symmetrical. Depending on the application only half of the
matrix may be used.
b. Adjacency list structure

Each vertex is associated with a list, that holds all adjacent vertices.
A: B, C
B: A, D
C: A, D
D: B, C
Other information, as weights, labels, names, can be represented using auxiliary arrays.
Tree Traversal Spanning Trees:
In the mathematical field of graph theory, a spanning tree T of a connected, undirected graph G
is a tree composed of all the vertices and some (or perhaps all) of the edges of G. Informally, a
spanning tree of G is a selection of edges of G that form a tree spanning every vertex. That is,
every vertex lies in the tree, but no cycles (or loops) are formed. On the other hand, every bridge
of G must belong to T.
A spanning tree of a connected graph G can also be defined as a maximal set of edges of G that
contains no cycle, or as a minimal set of edges that connect all vertices.
Example:

A spanning tree (blue heavy edges) of a grid graph


Spanning forests
A spanning forest is a type of subgraph that generalises the concept of a spanning tree.
However, there are two definitions in common use. One is that a spanning forest is a subgraph
that consists of a spanning tree in each connected component of a graph. (Equivalently, it is a
maximal cycle-free subgraph.) This definition is common in computer science and optimisation.
It is also the definition used when discussing minimum spanning forests, the generalization to
disconnected graphs of minimum spanning trees. Another definition, common in graph theory, is
that a spanning forest is any subgraph that is both a forest (contains no cycles) and spanning
(includes every vertex).
There are a few general properties of spanning trees.
1. A connected graph can have more than one spanning tree. They can have as many
as where is the number of vertices in the graph.
2. All possible spanning trees for a graph G have the same number of edges and vertices.
3. Spanning trees do not have any cycles.

4. Spanning trees are all minimally connected. That is, if any one edge is removed, the
spanning tree will no longer be connected.

5. Adding any edge to the spanning tree will create a cycle. So, a spanning tree is
maximally acyclic.

6. Spanning trees have |n|-1 edges, where |n| is the number of vertices

Minimum Spanning Tree


A spanning tree with assigned weight less than or equal to the weight of every possible spanning
tree of a weighted, connected and undirected graph GG, it is called minimum spanning tree
(MST). The weight of a spanning tree is the sum of all the weights assigned to each edge of the
spanning tree.
Example

Kruskal's Algorithm
Kruskal's algorithm is a greedy algorithm that finds a minimum spanning tree for a connected
weighted graph. It finds a tree of that graph which includes every vertex and the total weight of
all the edges in the tree is less than or equal to every possible spanning tree.
Algorithm
Step 1 − Arrange all the edges of the given graph G(V,E)G(V,E) in non-decreasing order as per
their edge weight.
Step 2 − Choose the smallest weighted edge from the graph and check if it forms a cycle with the
spanning tree formed so far.
Step 3 − If there is no cycle, include this edge to the spanning tree else discard it.
Step 4 − Repeat Step 2 and Step 3 until (V−1)(V−1) number of edges are left in the spanning
tree.
Problem
Suppose we want to find minimum spanning tree for the following graph G using Kruskal’s
algorithm.

Solution
From the above graph we construct the following table −
Edge No. Vertex Pair Edge Weight
E1 (a, b) 20
E2 (a, c) 9
E3 (a, d) 13
E4 (b, c) 1
E5 (b, e) 4
E6 (b, f) 5
E7 (c, d) 2
E8 (d, e) 3
E9 (d, f) 14
Now we will rearrange the table in ascending order with respect to Edge weight −
Edge No. Vertex Pair Edge Weight
E4 (b, c) 1
E7 (c, d) 2
E8 (d, e) 3
E5 (b, e) 4
E6 (b, f) 5
E2 (a, c) 9
E3 (a, d) 13
E9 (d, f) 14
E1 (a, b) 20
Since we got all the 5 edges in the last figure, we stop the algorithm and this is the minimal
spanning tree and its total weight is (1+2+3+5+9)=20(1+2+3+5+9)=20.
Prim's Algorithm
Prim's algorithm, discovered in 1930 by mathematicians, Vojtech Jarnik and Robert C. Prim, is a
greedy algorithm that finds a minimum spanning tree for a connected weighted graph. It finds a
tree of that graph which includes every vertex and the total weight of all the edges in the tree is
less than or equal to every possible spanning tree. Prim’s algorithm is faster on dense graphs.
Algorithm
 Initialize the minimal spanning tree with a single vertex, randomly chosen from the
graph.

 Repeat steps 3 and 4 until all the vertices are included in the tree.

 Select an edge that connects the tree with a vertex not yet in the tree, so that the weight of
the edge is minimal and inclusion of the edge does not form a cycle.

 Add the selected edge and the vertex that it connects to the tree.

Problem
Suppose we want to find minimum spanning tree for the following graph G using Prim’s
algorithm.

Solution
Here we start with the vertex ‘a’ and proceed.

You might also like