M5
M5
Representation of Graphs:
There are two different sequential representations of a graph. They are Adjacency Matrix
representation and Path Matrix representation
Adjacency Matrix Representation
Suppose G is a simple directed graph with m nodes, and suppose the nodes of G have been
ordered and are called v1, v2, . . . , vm. Then the adjacency matrix A = (aij) of the graph G is
the m x m matrix defined as follows:
1 if vi is adjacent to Vj, that is, if there is an edge (Vi, Vj) aij =0 otherwise
Suppose G is an undirected graph. Then the adjacency matrix A of G will be a symmetric
matrix, i.e., one in which aij = aji; for every i and j.
Drawbacks
• It may be difficult to insert and delete nodes in G.
• If the number of edges is 0(m) or 0(m log2 m), then the matrix A will be sparse, hence
a great deal of space will be wasted.
Path Matrix Representation
Let G be a simple directed graph with m nodes, v1,v2, . . . ,vm. The path matrix of G is the
m-square matrix P = (pij) defined as follows:
1 if there is a path from Vi to Vj Pij =0 otherwise
Isomorphism:
Let G1 and G1 be two graphs and let f be a function from the vertex set of G1 to the vertex
set of G2. Suppose that f is one-to-one and onto & f(v) is adjacent to f(w) in G2 if and only if
v is adjacent to w in G1.
Then we say that the function f is an isomorphism and that the two graphs G1 and G2 are
isomorphic. So two graphs G1 and G2 are isomorphic if there is a one-to-one correspondence
between vertices of G1 and those of G2 with the property that if two vertices of G1 are
adjacent then so are their images in G2. If two graphs are isomorphic then as far as we are
concerned they are the same graph though the location of the vertices may be different.
Example:
ƒ(a) = 1
ƒ(b) = 6
ƒ(c) = 8
ƒ(d) = 3
ƒ(g) = 5
ƒ(h) = 2
ƒ(i) = 4
ƒ(j) = 7
Euler circuits:
In graph theory, an Eulerian trail is a trail in a graph which visits every edge exactly once.
Similarly, an Eulerian circuit is an Eulerian trail which starts and ends on the same vertex.
They were first discussed by Leonhard Euler while solving the famous Seven Bridges of
Königsberg problem in 1736. Mathematically the problem can be stated like this:
Given the graph on the right, is it possible to construct a path (or a cycle, i.e. a path starting
and ending on the same vertex) which visits each edge exactly once?
Euler proved that a necessary condition for the existence of Eulerian circuits is that all
vertices in the graph have an even degree, and stated without proof that connected graphs
with all vertices of even degree have an Eulerian circuit. The first complete proof of this
latter claim was published in 1873 by Carl Hierholzer.
The term Eulerian graph has two common meanings in graph theory. One meaning is a
graph with an Eulerian circuit, and the other is a graph with every vertex of even degree.
These definitions coincide for connected graphs.
For the existence of Eulerian trails it is necessary that no more than two vertices have an odd
degree; this means the Königsberg graph is not Eulerian. If there are no vertices of odd
degree, all Eulerian trails are circuits. If there are exactly two vertices of odd degree, all
Eulerian trails start at one of them and end at the other. Sometimes a graph that has an
Eulerian trail but not an Eulerian circuit is called semi-Eulerian.
An Eulerian trail, Eulerian trail or Euler walk in an undirected graph is a path that uses
each edge exactly once. If such a path exists, the graph is called traversable or semi-
eulerian.
An Eulerian cycle, Eulerian circuit or Euler tour in an undirected graph is a cycle that uses
each edge exactly once. If such a cycle exists, the graph is called unicursal. While such
graphs are Eulerian graphs, not every Eulerian graph possesses an Eulerian cycle.
For directed graphs path has to be replaced with directed path and cycle with directed cycle.
The definition and properties of Eulerian trails, cycles and graphs are valid for multigraphs as
well.
Every vertex of this graph has an even degree, therefore this is an Eulerian graph. Following
the edges in alphabetical order gives an Eulerian circuit/cycle.
Hamiltonian graphs:
In the mathematical field of graph theory, a Hamiltonian path (or traceable path) is a path
in an undirected graph which visits each vertex exactly once. A Hamiltonian cycle (or
Hamiltonian circuit) is a cycle in an undirected graph which visits each vertex exactly once
and also returns to the starting vertex. Determining whether such paths and cycles exist in
graphs is the Hamiltonian path problem which is NP-complete.
Hamiltonian paths and cycles are named after William Rowan Hamilton who invented the
Icosian game, now also known as Hamilton's puzzle, which involves finding a Hamiltonian
cycle in the edge graph of the dodecahedron. Hamilton solved this problem using the Icosian
Calculus, an algebraic structure based on roots of unity with many similarities to the
quaternions (also invented by Hamilton). This solution does not generalize to arbitrary
graphs.
A Hamiltonian path or traceable path is a path that visits each vertex exactly once. A graph
that contains a Hamiltonian path is called a traceable graph. A graph is Hamilton-
connected if for every pair of vertices there is a Hamiltonian path between the two vertices.
A Hamiltonian cycle, Hamiltonian circuit, vertex tour or graph cycle is a cycle that visits
each vertex exactly once (except the vertex which is both the start and end, and so is visited
twice). A graph that contains a Hamiltonian cycle is called a Hamiltonian graph.
Similar notions may be defined for directed graphs, where each edge (arc) of a path or cycle
can only be traced in a single direction (i.e., the vertices are connected with arrows and the
edges traced "tail-to-head").
A Hamiltonian decomposition is an edge decomposition of a graph into Hamiltonian
circuits.
Examples
• a complete graph with more than two vertices is Hamiltonian
• every cycle graph is Hamiltonian
• every tournament has an odd number of Hamiltonian paths
• every platonic solid, considered as a graph, is Hamiltonian
Planar Graphs:
In graph theory, a planar graph is a graph that can be embedded in the plane, i.e., it can be
drawn on the plane in such a way that its edges intersect only at their endpoints.
A planar graph already drawn in the plane without edge intersections is called a plane graph
or planar embedding of the graph. A plane graph can be defined as a planar graph with a
mapping from every node to a point in 2D space, and from every edge to a plane curve, such
that the extreme points of each curve are the points mapped from its end nodes, and all curves
are disjoint except on their extreme points. Plane graphs can be encoded by combinatorial
maps.
It is easily seen that a graph that can be drawn on the plane can be drawn on the sphere as
well, and vice versa.
The equivalence class of topologically equivalent drawings on the sphere is called a planar
map. Although a plane graph has an external or unbounded face, none of the faces of a
planar map have a particular status.
Applications
Telecommunications – e.g. spanning trees
Vehicle routing – e.g. planning routes on roads without underpasses VLSI – e.g.
laying out circuits on computer chip.
The puzzle game Planarity requires the player to "untangle" a planar graph so that none of
its edges intersect.
Example graphs
Planar non planar
Chromatic Numbers:
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.
Directed Graphs
A directed graph G, also called a digraph or graph is the same as a multigraph except that
each edge e in G is assigned a direction, or in other words, each edge e is identified with an
ordered pair (u, v) of nodes in G.
Indegree : The indegree of a vertex is the number of edges for which v is head
Outdegree :The outdegree of a node or vertex is the number of edges for which v is tail.
Example
Outdegree of 1 =1
Outdegree of 2 =2
Indegree of 1=1
Indegree of 2 = 2
Simple Directed Graph
A directed graph G is said to be simple if G has no parallel edges. A simple graph G may
have loops, but it cannot have more than one loop at a given node.
Directed Acyclic Graph (DAG)
A directed acyclic graph (DAG) is a finite directed graph with no directed cycles. That is, it
consists of finitely many vertices and edges (also called arcs), with each edge directed from
one vertex to another, such that there is no way to start at any vertex v and follow a
consistently-directed sequence of edges that eventually loops back to v again. Equivalently, a
DAG is a directed graph that has a topological ordering, a sequence of the vertices such that
every edge is directed from earlier to later in the sequence. Every directed acyclic graph has
a topological ordering, an ordering of the vertices such that the starting endpoint of every
edge occurs earlier in the ordering than the ending endpoint of the edge. The existence of
such an ordering can be used to characterize DAGs: a directed graph is a DAG if and only if
it has a topological ordering.
Labeled or Weighted Graph
If the weight is assigned to each edge of the graph then it is called as Weighted or Labeled
graph.
The definition of a graph may be generalized by permitting the following:
• Multiple edges: Distinct edges e and e' are called multiple edges if they connect the
same endpoints, that is, if e = [u, v] and e' = [u, v].
• Loops: An edge e is called a loop if it has identical endpoints, that is, if e = [u, u].
• Finite Graph:A multigraph M is said to be finite if it has a finite number of nodes
and a finite number of edges.
Trees:
A tree is an undirected graph in which any two vertices are connected by exactly one path.
Every acyclic connected graph is a tree, and vice versa. A forest is a disjoint union of trees,
or equivalently an acyclic graph that is not necessarily connected.
A tree is an undirected graph G that satisfies any of the following equivalent conditions:
• G is connected and acyclic (contains no cycles).
• G is acyclic, and a simple cycle is formed if any edge is added to G.
• G is connected, but would become disconnected if any single edge is removed from G.
• G is connected and the 3-vertex complete graph K3 is not a minor of G.
• Any two vertices in G can be connected by a unique simple path.
If G has finitely many vertices, say n of them, then the above statements are also equivalent
to any of the following conditions:
• G is connected and has n − 1 edges.
• G is connected, and every subgraph of G includes at least one vertex with zero or one
incident edges. (That is, G is connected and 1-degenerate.)
• G has no simple cycles and has n − 1 edges.
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.
Given an undirected and connected graph G=(V,E), a spanning tree of the graph G is a tree
that spans G(that is, it includes every vertex of G) and is a subgraph of G (every edge in the
tree belongs to G)
Minimum Spanning Tree
The cost of the spanning tree is the sum of the weights of all the edges in the tree. There can
be many spanning trees. Minimum spanning tree is the spanning tree where the cost is
minimum among all the spanning trees. There also can be many minimum spanning trees.
There are two famous algorithms for finding the Minimum Spanning Tree:
Kruskal’s Algorithm
Kruskal’s Algorithm builds the spanning tree by adding edges one by one into a growing
spanning tree. Kruskal's algorithm follows greedy approach as in each iteration it finds an
edge which has least weight and add it to the growing spanning tree.
Algorithm Steps:
• Sort the graph edges with respect to their weights.
• Start adding edges to the MST from the edge with the smallest weight until the edge
of the largest weight.
• Only add edges which doesn't form a cycle , edges which connect only disconnected
components.
In Kruskal’s algorithm, at each iteration we will select the edge with the lowest weight. So,
we will start with the lowest weighted edge first i.e., the edges with weight 1. After that we
will select the second lowest weighted edge i.e., edge with weight 2. Notice these two edges
are totally disjoint. Now, the next edge will be the third lowest weighted edge i.e., edge with
weight 3, which connects the two disjoint pieces of the graph. Now, we are not allowed to
pick the edge with weight 4, that will create a cycle and we can’t have any cycles. So we will
select the fifth lowest weighted edge i.e., edge with weight 5. Now the other two edges will
create cycles so we will ignore them. In the end, we end up with a minimum spanning tree
with total cost 11 ( = 1 + 2 + 3 + 5).
Prim’s Algorithm
Prim’s Algorithm also use Greedy approach to find the minimum spanning tree. In Prim’s
Algorithm we grow the spanning tree from a starting position. Unlike an edge in Kruskal's,
we add vertex to the growing spanning tree in Prim's.
Algorithm Steps:
• Maintain two disjoint sets of vertices. One containing vertices that are in the growing
spanning tree and other that are not in the growing spanning tree.
• Select the cheapest vertex that is connected to the growing spanning tree and is not in
the growing spanning tree and add it into the growing spanning tree. This can be done
using Priority Queues. Insert the vertices, that are connected to growing spanning tree,
into the Priority Queue.
• Check for cycles. To do that, mark the nodes which have been already selected and
insert only those nodes in the Priority Queue that are not marked.
In Prim’s Algorithm, we will start with an arbitrary node (it doesn’t matter which one) and
mark it. In each iteration we will mark a new vertex that is adjacent to the one that we have
already marked. As a greedy algorithm, Prim’s algorithm will select the cheapest edge and
mark the vertex. So we will simply choose the edge with weight 1. In the next iteration we
have three options, edges with weight 2, 3 and 4. So, we will select the edge with weight 2
and mark the vertex. Now again we have three options, edges with weight 3, 4 and 5. But we
can’t choose edge with weight 3 as it is creating a cycle. So we will select the edge with
weight 4 and we end up with the minimum spanning tree of total cost 7 ( = 1 + 2 +4).