Graph Data Structure
Graph Data Structure
Abstract
Graph Data Structure is a non-linear data structure consisting of
vertices and edges. This article explores the essential aspects of
graph data structures, including their types, representations, traversal
algorithms, and real-world applications. By understanding graphs, one
can solve complex problems efficiently and model various scenarios in
computing.
For directed graphs, the matrix may not be symmetric since the edge
from node i to node j might be different from the edge from node j to
node i.
Space Complexity:
● O(V^2)
● The matrix requires space proportional to the square of the
number of vertices (V). This can be inefficient for sparse graphs
with few edges.
Time Complexity:
● O(V + E)
● The space used is proportional to the number of vertices (V) plus
the number of edges (E). This is more space-efficient for sparse
graphs.
Time Complexity:
Algorithm
1. Start with an initial node as the current node.
2. Mark the current node as visited to keep track of the nodes
already explored.
3. Process the current node (e.g., print its value or perform any
desired operation).
4. Find an unvisited neighboring node of the current node and
make it the new current node.
5. If all neighboring nodes are visited, backtrack to the previous
node (pop it from the stack if using an explicit stack).
6. Repeat steps 2 to 5 until there are no more unvisited nodes.
Time Complexity: O(V + E)
Algorithm -:
1. Pick a starting node, push it into the queue, mark it as visited.
2. In every iteration, pop out the 'x' node and put it in the solution
vector.
3. All the unvisited adjacent nodes from 'x' are pushed into the
queue.
4. Repeat steps 2 and 3.
Time Complexity: O(V + E)
● The space is used by the queue and visited list, where V is the
number of vertices.