Professional Documents
Culture Documents
DSA5
DSA5
Prathap D L
Dept Of CSE(MCA)
VTU PG Centre. Mysuru
Graphs: Definitions, Terminologies, Matrix and Adjacency List Representation Of
Graphs, Elementary Graph operations, Traversal methods: Breadth First Search and
Depth First Search. Insertion Sort, Radix sort, Address Calculation Sort. Hash Table
organizations, Hashing Functions, Static and Dynamic Hashing.
Graphs:
• Graph is a non linear data structure
• A graph consists of two sets:
• V: A finite set of vertices (also called nodes).
• E: A set of edges, which are pairs of vertices connecting them.
• We can represent a graph G by G = (V, E), where V are the vertices and E are the edges.
Undirected Graphs:
• Edges in undirected graphs are unordered. This means (v1, v2) and (v2, v1) represent the same connection.
• Imagine a two-way street: you can travel on it in either direction.
Directed Graphs:
• Edges in directed graphs are ordered pairs (v1, v2).
• v1 is the tail (source) of the edge.
• v2 is the head (destination) of the edge.
• (v1, v2) and (v2, v1) represent different connections.
• Imagine a one-way street: you can only travel on it in the specified direction.
The following is a graph with 5 vertices and 6 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and
E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
Graph Terminology
Vertex
Individual data element of a graph is called as Vertex. Vertex is also known as node. In above example graph, A, B, C, D
& E are known as vertices.
Edge
An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is represented as (starting Vertex,
ending Vertex). For example, in above graph the link between vertices A and B is represented as (A,B). In above example
graph, there are 7 edges (i.e., (A,B), (A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).
Directed Graph: A graph with only directed edges is said to be directed graph.
Mixed Graph:
A graph with both undirected and directed edges is said to be mixed graph.
Origin:
If a edge is directed, its first endpoint is said to be the origin of it.
Destination:
If a edge is directed, its first endpoint is said to be the origin of it and the other endpoint is said to be
the destination of that edge.
Adjacent:
If there is an edge between vertices A and B then both A and B are said to be adjacent. In other words,
vertices A and B are said to be adjacent if there is an edge between them.
Incident: Edge is said to be incident on a vertex if the vertex is one of the endpoints of that edge.
Outgoing: Edge A directed edge is said to be outgoing edge on its origin vertex.
Incoming: Edge A directed edge is said to be incoming edge on its destination vertex.
Degree :Total number of edges connected to a vertex is said to be degree of that vertex.
Indegree :Total number of incoming edges connected to a vertex is said to be indegree of that vertex.
Outdegree :Total number of outgoing edges connected to a vertex is said to be outdegree of that
vertex.
Simple Graph: A graph is said to be simple if there are no parallel and self-loop edges.
Path:
A path is a sequence of alternate vertices and edges that starts at a vertex and ends at other vertex such that
each edge is incident to its predecessor and successor vertex.
Matrix and Adjacency List Representation Of Graphs
We can easily represent the graphs using the following ways,
1. Adjacency matrix
2. Adjacency list
Adjacency Matrix:
An Adjacency Matrix is the simplest way to represent a graph. It is a 2D array of V x
V vertices, with each row and column representing a vertex. The matrix consists of “0” or “1”.
0 depicts that there is no path, while 1 represents that there is a path.
0 1 2 3 4
0 0 1 1 1 0
1 1 0 0 1 1
2 1 0 0 1 0
3 1 1 1 0 1
4 0 1 0 1 0
Adjacency Matrix of Undirected Graph
Adjacency Matrix of Directed Graph
Adjacency List:
It represents a graph as an array (A) of linked lists. The vertices are stored as an index of the
one-dimensional array, and the edges are stored as a list. It means that each element of the array Ai is a
list. It contains all the vertices adjacent to vertex i.
Adjlist[0] will have all the nodes which are connected to vertex 0.
Adjlist[1] will have all the nodes which are connected to vertex 1 and so on.
Inverse adjacency lists are useful for finding the in-degree of a vertex in directed graphs.
The in-degree of a vertex is the number of edges coming into it.
There is one list for each vertex in the graph.
In the case of adjacency list ,the total number of list node is 2e,because one edge contribute 2 nodes.
For reducing the number of list nodes, we introduce adjacency Multilist.
The list node Structure is
So each head node contains only ‘e’ number of list nodes, not 2e.
N is a mark bit used for analyze the edge.
Path1 contains the next information about 1
Path 2 contains the next information about 2