Professional Documents
Culture Documents
A Graph Consists of A Finite Set of Vertices (Or Nodes) and Set of Edges Which Connect A Pair of Nodes
A Graph Consists of A Finite Set of Vertices (Or Nodes) and Set of Edges Which Connect A Pair of Nodes
A Graph is a non-linear data structure consisting of nodes and edges. The nodes are
sometimes also referred to as vertices and the edges are lines or arcs that connect
any two nodes in the graph. More formally a Graph can be defined as,
A Graph consists of a finite set of vertices(or nodes) and set of Edges which connect
a pair of nodes.
In the above Graph, the set of vertices V = {0,1,2,3,4} and the set of edges E = {01,
12, 23, 34, 04, 14, 13}.
Graphs are used to solve many real-life problems. 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, locale etc.
• Web page linking — The graph nodes are web pages, and the edges represent
hyperlinks between pages.
• Airports — The graph nodes are airports, and the edges represent flights
between airports.
In the example above, the graph can be traversed from vertex A to B, but not from
vertex B to A.
Undirected Graphs
In the example above, the graph can be traversed from node A to B as well as
from node B to A.
Some more complex directed and undirected graphs might look like the following:
Adjacency Matrix:
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.
The adjacency matrix for the above example graph is:
Path Matrix
Let G be a graph with m edges, and u and v be any two vertices in G. The path
matrix for vertices u and v denoted by P(u, v) = [pi j]q×m, where q is the number of
different paths between u and v, is defined as
Clearly, a path matrix is defined for a particular pair of vertices, the rows in P(u,
v) correspond to different paths between u and v, and the columns correspond to
different edges in G. For example, consider the graph in the figure below
The different paths between the vertices v3 and v4 are
p1 = {e8, e5}, p2 = {e8, e7, e3} and p3 = {e8, e6, e4, e3}.
The path matrix for v3, v4 is given by
Basic Operations
Following are basic primary operations of a Graph −
Add Vertex − Adds a vertex to the graph.
Add Edge − Adds an edge between the two vertices of the graph.
Display Vertex − Displays a vertex of the graph.