0
0
Data-structures-theoryHow-ToBeginner ยท 4 min read

Applications of Graph Data Structures: Key Uses Explained

Graphs are used to model relationships and connections in data. Common applications include social networks to represent friendships, routing algorithms for maps and navigation, and dependency graphs in software and task scheduling.
๐Ÿ“

Syntax

A graph is made of nodes (also called vertices) and edges connecting them. It can be directed (edges have direction) or undirected. Edges can have weights representing cost or distance.

Basic graph representation in code:

  • Graph = { nodes: [...], edges: [...] }
  • Edges can be pairs like (node1, node2) or triples (node1, node2, weight)
python
class Graph:
    def __init__(self):
        self.nodes = set()
        self.edges = {}

    def add_node(self, value):
        self.nodes.add(value)
        self.edges[value] = []

    def add_edge(self, from_node, to_node, weight=1):
        self.edges[from_node].append((to_node, weight))
๐Ÿ’ป

Example

This example shows a simple graph representing a city map with locations as nodes and roads as edges with distances as weights. It demonstrates how to add nodes and edges.

python
class Graph:
    def __init__(self):
        self.nodes = set()
        self.edges = {}

    def add_node(self, value):
        self.nodes.add(value)
        self.edges[value] = []

    def add_edge(self, from_node, to_node, weight=1):
        self.edges[from_node].append((to_node, weight))

# Create graph
city_map = Graph()

# Add locations
city_map.add_node('A')
city_map.add_node('B')
city_map.add_node('C')

# Add roads with distances
city_map.add_edge('A', 'B', 5)
city_map.add_edge('B', 'C', 10)
city_map.add_edge('A', 'C', 15)

# Print edges
for node in city_map.nodes:
    print(f"From {node}: {city_map.edges[node]}")
Output
From A: [('B', 5), ('C', 15)] From B: [('C', 10)] From C: []
โš ๏ธ

Common Pitfalls

Common mistakes when working with graphs include:

  • Confusing directed and undirected edges, which changes how connections work.
  • Forgetting to add nodes before adding edges, causing errors.
  • Not handling cycles properly in algorithms, which can cause infinite loops.
  • Ignoring edge weights when they matter, leading to wrong shortest path results.
python
graph = Graph()
# Wrong: adding edge before nodes
# graph.add_edge('X', 'Y')  # This will cause an error because nodes don't exist yet

# Right way:
graph.add_node('X')
graph.add_node('Y')
graph.add_edge('X', 'Y')
๐Ÿ“Š

Quick Reference

ApplicationDescriptionExample
Social NetworksModel users and their connectionsFacebook friends graph
Routing & NavigationFind shortest paths between locationsGoogle Maps directions
Dependency ManagementTrack dependencies between tasks or modulesBuild systems like Make
Recommendation SystemsSuggest items based on user-item graphsNetflix movie recommendations
Network AnalysisAnalyze communication or transport networksInternet topology mapping
โœ…

Key Takeaways

Graphs represent relationships using nodes and edges, useful for connected data.
They are essential in social networks, routing, and dependency tracking.
Always add nodes before edges to avoid errors.
Understand if your graph is directed or undirected to apply correct logic.
Edge weights matter for algorithms like shortest path and must be handled carefully.