Graph
Graph
Graphs are non linear data structure. A graph is made up of sets of nodes and lines. Nodes are called vertices. Lines are called edges. Lines are used to connect nodes.
Types of Graph
(1) Undirected Graph
An undirected graph has edges that have no direction. Undirected graph also called an undigraph.
(2) Directed Graph
A directed graph has edges that are unidirectional. Directed graph also called digraph.
(3) Weighted Graph
A weighted graph has weights associated with each edge. The weight can represent distances costs.
(4) Complete Graph
A complete graph if there is an edge between every pair of vertices. In complete graph every node should be connected to all other nodes.
(5) Acyclic Graph
A directed graph that has no cycles are called acyclic graph.
Terminologies of a Graph
- Degree of Node
The number of edges that a node contains is called degree of the node.
Here A has a degree of 3 node D has a degree of 2 node.
- Out degree
The number of edges beginning from a node is called the out degree of the node.
- Outdegree of A is 3.
- Outdegree of F is 0.
- In degree
The number of edges ending at a node is called in degree.
- Indegree of E is 3.
- Indegree of F is 2.
- Source node (Start node)
The node that has a positive out degree but zero in degree.
Node A is the source node. Outdegree is 3 and indegree is 0.
- Sink node (End node)
The node that has a positive in degree but zero out degree.
- Node E is the sink node.
- Indegree is 3 and outdegree is 0.
- Path
A list of nodes of a graph where each node has an edge from it to the next node is called the path.
- Length
The number of edges in a path of a graph is called length of the graph.
- Loop Edge
An edge A is said to be a loop edge if it has the same node at its tail and head.
Representation of a Graph
- Adjacency Matrix
It is a table which represents the edges between vertices of the graph.
| A | B | C | D |
A | 0 | 1 | 1 | 1 |
B | 0 | 0 | 0 | 1 |
C | 0 | 0 | 0 | 1 |
D | 0 | 0 | 1 | 0 |
- Adjacency List
This is any array of linked list, one for each vertex. Each linked list contains all of the vertices that are adjacent to a given vertex.
Traversing of a Graph
(1) BFS
BFS stand for Breadth first search. Breadth first search uses FIFO queue. It start several paths at a time and advance in each one step at a time.
Example
BFS = A ,B ,C ,D ,E ,F ,G ,H
BFS = 1 ,3 ,4 ,2 ,7 ,9
(2) DFS
DFS stand for Depth first search. Depth first search uses LIFO stack. In DFS , once a possible path is found it continue search until the end of the path.
Example
DFS = A ,B ,D ,H ,E ,C ,F ,G
👍🏻
ReplyDelete