Graphs Graphs are very important data structure.
The graph data structure represents entities (nodes/vertices) and their relationships (edges), and it has many real-life equivalents where connections or networks matter. Common real-world examples include:
Social Networks (e.g., Facebook, LinkedIn): People are nodes, and friendships or connections are edges.
Road Maps or City Navigation: Locations (cities, intersections) are nodes, and roads are edges. Navigation apps use graph algorithms like Dijkstra’s.
Internet/Web: Webpages are nodes, and hyperlinks are edges—forming a massive directed graph.
There are 2 major categories of graph:
Weighted graph: Where the weights of the edges are to be considered. Eg. distance or time taken to travel between 2 nodes. Capacity of a route in a shop floor.
Unweighted graph: Where are no weights in the relationships. Eg. Facebook friends, Twitter followers or Internet web page links
There are 2 main ways of storing graph data:
Adjacency List: Its very useful where the relationships between vertices / nodes are very sparse. It helps storing information more efficiently. Space complexity and iterating all edges takes O(N + E).
Adjacency Graph: It is quite useful in dense data relationships where in almost all the nodes are connected to each other. It is quite easy to store and visualize the data in the form of a 2 dimensional matrix. Space complexity and iterating all edges takes is O(N^2)