Graphs
Last updated
Last updated
A Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph. More formally, a Graph is composed of a set of vertices (V) and a set of edges (E). The graph is denoted by G (E, V).
There are several types of graphs in data structures:
Undirected Graphs: Edges have no direction, i.e., the edges do not have arrows indicating the direction of traversal.
Directed Graphs: Edges have a direction, i.e., the edges have arrows indicating the direction of traversal.
Weighted Graphs: Edges have weights or costs associated with them.
Unweighted Graphs: Edges have no weights or costs associated with them.
Complete Graphs: Each vertex is connected to every other vertex.
Bipartite Graphs: The vertices can be divided into two disjoint sets such that every edge connects a vertex in one set to a vertex in the other set.
Trees: A connected graph with no cycles.
Cycles: A graph with at least one cycle.
Sparse Graphs: A graph with relatively few edges compared to the number of vertices.
Dense Graphs: A graph with many edges compared to the number of vertices.
Finite Graphs: A graph with a finite number of vertices and edges.
Infinite Graphs: A graph with an infinite number of vertices and edges.
Trivial Graph: A graph with only one vertex and no edges.
Multi Graph: A graph which may have multiple edges (also called parallel edges), i.e., edges that have the same end nodes. Thus, two vertices may be connected by more than one edge.
Null Graph: A graph is known as a null graph if there are no edges in the graph.
There are several operations that can be performed on graphs in data structures:
Add Vertex: This operation adds a vertex to the graph.
Add Edge: This operation adds an edge between two vertices in the graph.
Remove Vertex: This operation removes a vertex from the graph.
Remove Edge: This operation removes an edge between two vertices in the graph.
Query: This operation checks if there is an edge between two vertices.
Neighbors: This operation lists all vertices connected to a given vertex.
Search: This operation searches for an entity in the graph.
Traversal: This operation traverses all the nodes in the graph.
Operation | Adjacency Matrix | Adjacency List |
---|---|---|
Access
O(1)
**O(
Search
**O(
V
Insertion
**O(
V
Deletion
**O(
V