0% found this document useful (0 votes)
15 views5 pages

Graphs

Uploaded by

pranshusahu862
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Graphs

Uploaded by

pranshusahu862
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Graphs in Data Structures

1. Introduction to Graphs
A graph is a fundamental data structure in computer science, consisting of:
• Vertices (or Nodes): Represent entities.
• Edges: Represent the relationships between vertices.
Graphs are used to model networks, relationships, and interactions in various fields, such as social
networks, transportation systems, and the internet.

Formal Definition
A graph G is defined as: G=(V,E) Where:
• V is the set of vertices.
• E is the set of edges, represented as pairs (u,v) where u,v∈V.

2. Types of Graphs
Graphs can be classified based on their properties:

2.1 Directed and Undirected Graphs


• Directed Graph (Digraph): Edges have a direction, represented as (u→v).
• Undirected Graph: Edges have no direction, represented as (u,v).

2.2 Weighted and Unweighted Graphs


• Weighted Graph: Edges have weights or costs associated with them.
• Unweighted Graph: All edges have the same weight (or no weight).

2.3 Special Types of Graphs


1. Complete Graph: Every pair of vertices is connected by an edge.
2. Connected Graph: There is a path between every pair of vertices.
3. Disconnected Graph: At least one pair of vertices is not connected.
4. Cyclic Graph: Contains at least one cycle.
5. Acyclic Graph: Contains no cycles.
6. Bipartite Graph: Vertices can be divided into two disjoint sets, and edges connect vertices
from different sets.

2.4 Trees
A tree is a special type of graph that is connected and acyclic.
3. Graph Representation
Graphs can be represented in memory using different methods:

3.1 Adjacency Matrix


A 2D matrix where the element at [i][j] indicates the presence (and possibly weight) of an edge
between vertices i and j.
Advantages:
• Simple to implement.
• Efficient for dense graphs.
Disadvantages:
• Consumes more memory for sparse graphs.

3.2 Adjacency List


Each vertex has a list of adjacent vertices (or edges).
Advantages:
• Space-efficient for sparse graphs.
• Easier to iterate over neighbors.
Disadvantages:
• More complex to implement.

3.3 Edge List


A list of all edges, each represented as a pair (or triplet for weighted graphs).
Advantages:
• Simple for graph algorithms like Kruskal's MST.
• Compact representation.

4. Graph Traversal
Graph traversal is the process of visiting all vertices in a graph.

4.1 Depth-First Search (DFS)


• Explores as far as possible along each branch before backtracking.
• Implemented using recursion or a stack.
Algorithm:
1. Start at the root (or any arbitrary node).
2. Visit and mark the current node.
3. Recursively visit unvisited neighbors.
Example Code (Python):
python
Copy code
def dfs(graph, node, visited):
if node not in visited:
print(node, end=" ")
visited.add(node)
for neighbor in graph[node]:
dfs(graph, neighbor, visited)

4.2 Breadth-First Search (BFS)


• Explores all neighbors at the current depth before moving to the next depth level.
• Implemented using a queue.
Algorithm:
1. Start at the root (or any arbitrary node).
2. Enqueue the starting node.
3. Dequeue a node, visit it, and enqueue its unvisited neighbors.
Example Code (Python):
python
Copy code
from collections import deque

def bfs(graph, start):


visited = set()
queue = deque([start])
while queue:
node = queue.popleft()
if node not in visited:
print(node, end=" ")
visited.add(node)
queue.extend(graph[node])

5. Graph Algorithms
5.1 Shortest Path Algorithms
1. Dijkstra's Algorithm:
• Finds the shortest path from a source to all other vertices in a weighted graph.
• Uses a priority queue.
2. Bellman-Ford Algorithm:
• Handles graphs with negative weights.
• Computes shortest paths by relaxing edges repeatedly.
3. Floyd-Warshall Algorithm:
• Finds shortest paths between all pairs of vertices.

5.2 Minimum Spanning Tree (MST) Algorithms


1. Kruskal's Algorithm:
• Greedy approach.
• Adds edges in ascending order of weights while avoiding cycles.
2. Prim's Algorithm:
• Greedy approach.
• Grows the MST by adding the smallest edge connecting a vertex in the tree to a
vertex outside it.

5.3 Topological Sorting


• Used for Directed Acyclic Graphs (DAGs).
• Orders vertices such that for every directed edge u→v, u appears before v.

6. Applications of Graphs
1. Social Networks: Modeling connections between individuals.
2. Transportation Networks: Optimizing routes in road and flight networks.
3. Web Graphs: Representing links between web pages.
4. Dependency Graphs: Scheduling tasks based on dependencies.
5. Network Flow: Solving problems like maximum flow in networks.

7. Complexity Analysis
Algorithm Time Complexity Space Complexity
DFS O(V+E) O(V)
BFS O(V+E) O(V)
Dijkstra’s Algorithm O(V2) or O((V+E)logV) (with a priority queue) O(V)
Kruskal’s Algorithm O(ElogE) O(V+E)
Prim’s Algorithm O(V2) or O((V+E)logV) O(V)

8. Real-World Example
Routing Algorithms
Graph-based routing algorithms like Dijkstra's are used in GPS navigation systems to find the
shortest path between two locations.

PageRank Algorithm
Google's PageRank algorithm models the web as a graph where pages are vertices, and hyperlinks
are directed edges.

9. Challenges in Graph Theory


1. Graph Coloring: Assigning colors to vertices such that no two adjacent vertices share the
same color.
2. Hamiltonian and Eulerian Paths: Finding paths that visit every vertex or edge exactly
once.
3. Planarity Testing: Determining if a graph can be drawn on a plane without edge crossings.

You might also like