Graph Data Structure
Graph Data Structure
Graph Data Structure
What is a Graph?
A graph is an ordered pair G = (V, E) comprising a set V of vertices or nodes and a
collection of pairs of vertices from V, known as edges of a graph. For example, for the
graph below.
V = { 1, 2, 3, 4, 5, 6 }
1. Undirected graph
An undirected graph (graph) is a graph in which edges have no orientation. The edge (x, y) is
identical to edge (y, x), i.e., they are not ordered pairs. The maximum number of edges
2. Directed graph
A Directed graph (digraph) is a graph in which edges have orientations, i.e., The edge (x, y)
Multiple edges are two or more edges that connect the same two vertices. A loop is an edge
5. Simple graph
A simple graph is an undirected graph in which both multiple edges and loops are disallowed as
opposed to a multigraph. In a simple graph with n vertices, every vertex’s degree is at most n-1.
In other words, an unweighted graph is a weighted graph with all edge weight as 1. Unless
present.
8. Connected graph
A Connected graph has a path between every pair of vertices. In other words, there are no
graphs are constructed. Each edge has two vertices to which it is attached, called
its endpoints.
● Two vertices are called adjacent if they are endpoints of the same edge.
● Outgoing edges of a vertex are directed edges that the vertex is the origin.
● Incoming edges of a vertex are directed edges that the vertex is the
destination.
● The degree of a vertex in a graph is the total number of edges incident to it.
● A vertex with in-degree zero is called a source vertex, while a vertex with out-
edge.
● Path is a sequence of alternating vertices and edges such that the edge connects
least 1.
unconnected graph.
● Tree is a connected graph with no cycles. If we remove all the cycles from DAG
(Directed Acyclic Graph), it becomes a tree, and if we remove any edge in a tree,
it becomes a forest.
Therefore, O(m) may vary between O(1) and O(n2), depending on how dense the graph is.
Graph Representation
matrix indicate whether pairs of vertices are adjacent or not in the graph.
Definition:
For a simple unweighted graph with vertex set V, the adjacency matrix is a square |V| × |V|
Each row in the matrix represents source vertices, and each column represents destination
vertices. The diagonal elements of the matrix are all zero since edges from a vertex to itself, i.e.,
loops are not allowed in simple graphs. If the graph is undirected, the adjacency matrix will be
symmetric. Also, for a weighted graph, Aij can represent edge weights.
An adjacency matrix keeps a value (1/0/edge-weight) for every pair of vertices, whether
the edge exists or not, so it requires n2 space. They can be efficiently used only when the graph
is dense.
collection of its neighboring vertices or edges, i.e., every vertex stores a list of adjacent vertices.
There are many variations of adjacency list representation depending upon the implementation.
This data structure allows the storage of additional data on the vertices but is practically very
efficient when the graph contains only a few edges. i.e. the graph is sparse.
THE SPANNING TREE
A spanning tree is a subset of Graph G, which has all the vertices covered with minimum
possible number of edges. Hence, a spanning tree does not have cycles and it cannot be
disconnected..
By this definition, we can draw a conclusion that every connected and undirected Graph G has at
least one spanning tree. A disconnected graph does not have any spanning tree, as it cannot be
spanned to all its vertices.
We found three spanning trees off one complete graph. A complete undirected graph can have
maximum nn-2 number of spanning trees, where n is the number of nodes. In the above addressed
example, n is 3, hence 33−2 = 3 spanning trees are possible.
We now understand that one graph can have more than one spanning tree. Following are a few
properties of the spanning tree connected to graph G −
Thus, we can conclude that spanning trees are a subset of connected Graph G and disconnected
graphs do not have spanning tree.
Let us understand this through a small example. Consider, city network as a huge graph and now
plans to deploy telephone lines in such a way that in minimum lines we can connect to all city
nodes. This is where the spanning tree comes into picture.
If number of vertices of a minimum spanning tree is |v| then number of edges will be |v|-1