Introduction to Graphs
Graph is composed of set of vertices(denoted as V) and set of edges(denoted as E).
The graph is denoted by G(E, V).
Components of a Graph
- Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are also known as vertex or nodes. Every node/vertex can be labelled or unlabelled.
- Edges: Edges are the drawn or used to connect two nodes of the graph. It can be ordered pair of nodes in a directed graph. Edges can connect any two nodes in any possible way. There are no rules. Sometimes, edges are also known as arcs. Every edge can be labelled/unlabelled.
- Graph having only a single vertex, it is the smallest graph possible.
- A graph in which edges do not have any direction. That is the nodes are unordered pairs in the definition of every edge.
- A graph in which edge has direction. That is the nodes are ordered pairs in the definition of every edge.
- The graph in which from one node we can visit any other node in the graph is known as a connected graph.
- The graph in which at least one node is not reachable from a node is known as a disconnected graph.
- The graph in which the degree of every vertex is equal to the other vertices of the graph. Let the degree of each vertex be K then the graph is called K-regular.
- The graph in which from each node there is an edge to each other node.
- A graph containing at least one cycle is known as a Cyclic graph.
Directed Acyclic Graph
- A graph in which vertex can be divided into two sets such that vertex in each set does not contain any edge between them.
1. A graph in which the edges are already specified with suitable weight is known as weighted graph.
2. Weighted graph can be further classified as directed weighted graph and undirected weighted graph.
Tree v/s Graph
Representation of Graphs
There are two ways to store a graph:
- Adjacency Matrix
- Adjacency List
Comparison between them
When the graph contains a large number of edges then it is good to store it as a matrix because only some entries in the matrix will be empty. An algorithm such as Prim’s and Dijkstra adjacency matrix is used to have less complexity.
|Action||Adjacency Matrix||Adjacency List|
|Removing and edge||O(1)||O(N)|
Steps in solving the problem
- Recognize a problem as a graph problem.
- Find an algorithm for solving the problem
- Design data structures and algorithms to implement the graph solution.
- Code It!
Basic Operations on Graphs
Below are the basic operations on the graph:
- Insertion of Nodes/Edges in graph – Insert a node into the graph.
- Deletion of Nodes/Edges in graph – Delete a node from graph.
- Searching on Graphs – Search an entity in graph.
- Traversal of Graphs – Traversing all the nodes in the graph.
Usage of graphs
- Maps can be represented using graph and this then can be used by computers to provide various services like the shortest path between two cities.
- When various tasks depend on each other then this situation can be represented using a Directed Acyclic graph and we can find the order in which task can be performed using topological sort.
- State Transition Diagram represents what can be the legal moves from current states. In-game of tic tac toe this can be used.
- Killing Interviews!