Graph Data Structure in C Language
Graph Data Structure in C Language
Structure in C
Language
Graphs are a fundamental data structure used in computer science
to represent relationships between objects. They consist of nodes
(vertices) and edges, which connect the nodes.
by Rup Kumar
Introduction to Graphs
1 Representing 2 Types of Graphs 3 Applications
Connections
Graphs can be directed, where Graphs are widely used in
Graphs are used to model edges have direction, or various algorithms, such as
relationships in real-world undirected, where edges have shortest path finding,
scenarios like social networks, no direction. minimum spanning trees, and
maps, and computer network flow.
networks.
Graph Terminology and Representation
Nodes (Vertices) Edges Adjacency List
Represent entities in the graph. Connect nodes, representing A common representation where
relationships between them. each node stores a list of its
neighbors.
Graph Traversal Algorithms:
Breadth-First Search (BFS)
Start at a Node
Begin by visiting the starting node.
Explore Neighbors
Visit all the unvisited neighbors of the current node.
Level-by-Level
BFS explores the graph level by level, visiting nodes closer
to the starting node first.
Graph Traversal Algorithms: Depth-First Search (DFS)
Start at a Node 1
Begin by visiting the starting node.
2 Explore Depth
Choose an unvisited neighbor and explore its depth,
continuing until a leaf node is reached.
Backtrack 3
When a leaf node is reached, backtrack to the previous
node and explore its other unvisited neighbors.
Graph Shortest Path Algorithms: Dijkstra's
Algorithm
Find the shortest path between Uses a greedy approach to Calculates the shortest path to
two nodes. iteratively select the node with each node from the source.
the shortest distance from the
source.
Graph Minimum Spanning Tree
Algorithms: Kruskal's Algorithm
Spanning Tree
Connects all nodes in a graph with a minimum number of edges.
Sort Edges
Sorts all edges in ascending order of their weights.
Avoid Cycles
Selects edges that don't form cycles in the tree.
Applications of Graphs in
Computer Science
Social Networks Maps
Representing connections Modeling routes and
and relationships between distances between
users. locations.