Professional Documents
Culture Documents
A graph is an abstract notation used to represent the connection between pairs of objects.
Graph is a data structure that consists of the following two components:
1. A finite set of vertices also called as nodes.
2. A finite set of ordered pair of the form (u, v) called as edge. The pair is ordered because (u, v) is not
same as (v, u) in case of a directed graph(di-graph). The pair of the form (u, v) indicates that there is
an edge from vertex u to vertex v. The edges may contain weight/value/cost.
A graph consists of
Vertices − Interconnected objects in a graph are called vertices. Vertices are also known as nodes.
Edges − Edges are the links that connect the vertices.
There are two types of graphs −
Directed graph − In a directed graph, edges have direction, i.e., edges go from one vertex to
another.
Undirected graph − In an undirected graph, edges have no direction.
Graphs are used to represent many real-life applications:
Graphs are used to represent networks. The networks may include paths in a city or telephone
network or circuit network.
Graphs are also used in social networks like linkedin, Facebook. For example, in Facebook,
each person is represented with a vertex (or node). Each node is a structure and contains
information like person id, name, gender and locale.
Graph Definitions
A graph G consists of two types of elements: vertices and edges. Each edge has two endpoints,
which belong to the vertex set. We say that the edge connects (or joins) these two vertices. The vertex
set of G is denoted V(G), or just V if there is no ambiguity. An edge between vertices u and v is written
as {u, v}. The edge set of G is denoted E(G), or just E if there is no ambiguity. The graph in this picture
has the vertex set V = {1, 2, 3, 4, 5, 6}. The edge set E = {{1, 2}, {1, 5}, {2, 3}, {2, 5}, {3, 4}, {4, 5},
{4, 6}}
Self Loop: A self-loop is an edge whose end points are a single vertex. Multiple edges are two or more
edges that join the same two vertices.
Multi Graph: A graph is called simple if it has no self-loops and no multiple edges, and a multi
graph if it does have multiple edges.
Trees: A tree is a connected simple acyclic graph. A vertex with degree 1 in a tree is called a leaf.
Directed graphs:
A directed graph or digraph G = (V, E) consists of a vertex set V and an edge set of ordered
pairs E of elements in the vertex set.
Here is a simple acyclic digraph (often called a DAG, “directed acyclic graph”) with seven vertices and
eight edges.
Adjacency matrix:
An adjacency matrix is a |V|x|V|-matrix of integers,
representing a graph G = (V, E).
The vertices are number from 1 to |V|.
The number at position (i, j) indicates the number of edges from i to j.
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot
adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Adjacency matrix for undirected graph is always
symmetric. Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i
to vertex j with weight w.
A spanning tree whose sum of weight (or length) of all its edges is less than all other possible spanning
tree of graph G is known as a minimal spanning tree or minimum cost spanning tree. The following
figure shows a weighted connected graph.
Some possible spanning trees of the above graph are shown below:
Among all the above spanning trees, figure (g) is the minimum spanning tree.
The concept of minimum cost spanning tree is applied in travelling salesman
problem, designing electronic circuits, designing efficient networks and designing
efficient routing algorithms. To implement the minimum cost-spanning tree, the
following two methods are used:
Prim’s Algorithm
Kruskal’s Algorithm