Graph
• A graphcan be defined as group of vertices
and edges that are used to connect these
vertices.
• A graph can be seen as a cyclic tree, where
the vertices (Nodes) maintain any complex
relationship among them instead of having
parent child relationship.
3.
• A graphG can be defined as an ordered set
G(V, E) where V(G) represents the set of
vertices and E(G) represents the set of edges
which are used to connect these vertices.
• A Graph G(V, E) with 5 vertices (A, B, C, D, E)
and six edges ((A,B), (B,C), (C,E), (E,D), (D,B),
(D,A)) is shown in the following figure.
4.
Graph Terminology
• Nodeor Vertex - The elements of a graph are
connected through edges.
• Edges - A path or a line between two vertices in a
graph.
• Adjacent Nodes - If two nodes u and v are
connected via an edge e, then the nodes u and v are
called as neighbors or adjacent nodes.
• Degree of the Node - A degree of a node is the
number of edges that are connected with that node.
A node with degree 0 is called as isolated node.
5.
Directed and UndirectedGraph
• A graph can be directed or undirected.
• In an undirected graph, edges are not associated with the
directions with them.
• An undirected graph is shown in the above figure since its
edges are not attached with any of the directions.
• If an edge exists between vertex A and B then the vertices can
be traversed from B to A as well as A to B.
6.
• In adirected graph, edges form an ordered
pair.
• Edges represent a specific path from some
vertex A to another vertex B.
• Node A is called initial node while node B is
called terminal node.
• A directed graph is shown in the following
figure.
7.
• Predecessor- Predecessorof a node mean the node
that immediately precedes that node.
Node 2 is a predecessor of node 1.
• Successor- Successor of a node mean the node that
immediately next of that node.
Node 1 is a successor of node 2.
• Indegree- Indegree of vertex V is the number of edges
which are coming into the vertex V. Notation − deg−
(V).
• Outdegree - Outdegree of vertex V is the number of
edges which are going out from the vertex V.
Notation − deg+
(V).
8.
Vertex 'a' hastwo edges, 'ad' and 'ab', which are going
outwards. Hence its outdegree is 2. Similarly, there is an edge
'ga', coming towards vertex 'a'. Hence the indegree of 'a' is 1.
9.
• Path- Apath can be defined as the sequence of nodes
that are followed in order to reach some terminal
node V from the initial node U.
Path(A, C) = { AB, BC }
• Closed Path - A path will be called as closed path if the
initial node is same as terminal node. A path will be
closed path if V0=VN.
• Simple Path- If all the nodes of the graph are distinct
with an exception V0=VN, then such path P is called as
closed simple path.
• Cycle -A cycle can be defined as the path which has no
repeated edges or vertices except the first and last
vertices.
10.
• Connected Graph- A connected graph is the
one in which some path exists between every
two vertices (u, v) in V. There are no isolated
nodes in connected graph.
Connected Graph
Not Connected Graph
11.
• Complete Graph- A complete graph is the one in which every
node is connected with all other nodes. A complete graph
contain n(n-1)/2 edges where n is the number of nodes in the
graph.
• Weighted Graph - In a weighted graph, each edge is assigned
with some data such as length or weight. The weight of an edge e
can be given as w(e) which must be a positive (+) value indicating
the cost of traversing the edge.
12.
• Length -Length of the graph is defined as the
number of edges contained in the graph.
Length of the graph: 8
AB, BC, CD, DE, EF, FA, AC, CE
13.
Representations of agraph
• In graph theory, a graph representation is a
technique to store graph into the memory of
computer.
• To represent a graph need the set of vertices, and for
each vertex the neighbors of the vertex (vertices
which is directly connected to it by an edge). If it is a
weighted graph, then the weight will be associated
with each edge.
• Adjacent matrix, adjacency list these representations
are commonly used.
14.
Adjacency Matrix
• Adjacencymatrix is a sequential representation.
• It is used to represent which nodes are adjacent
to each other. i.e. is there any edge connecting
nodes to a graph.
• In this representation, have to construct a nXn
matrix A. If there is any edge from a vertex i to
vertex j, then the corresponding element of A,
ai
,j
= 1, otherwise ai
,j
= 0.
• If there is any weighted graph then instead of 1s
and 0s, store the weight of the edge.
15.
Undirected graph representation
1represents an edge from row vertex to column vertex, and 0
represents no edge from row vertex to column vertex.
16.
Directed graph representation
1represents an edge from row vertex to column vertex, and 0
represents no edge from row vertex to column vertex.
Adjacency List
• Adjacencylist is a linked representation.
• In this representation, for each vertex in the
graph, maintain the list of its neighbors. It
means, every vertex of the graph contains list of
its adjacent vertices.
• We have an array of vertices which is indexed
by the vertex number and for each vertex v, the
corresponding array element points to a singly
linked list of neighbors of v.