0% found this document useful (0 votes)
28 views19 pages

Graph Data Structure

Uploaded by

fifan91796
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
28 views19 pages

Graph Data Structure

Uploaded by

fifan91796
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

Graph Data Structure

Abstract
Graph Data Structure is a non-linear data structure consisting of
vertices and edges. This article explores the essential aspects of
graph data structures, including their types, representations, traversal
algorithms, and real-world applications. By understanding graphs, one
can solve complex problems efficiently and model various scenarios in
computing.

Scope of the Article


This article focuses on graph data structures, including their types,
representations (adjacency matrix and list), and traversal algorithms
(DFS and BFS). It also examines real-world applications, outlining
how graphs are used to solve various computational problems.
Definitions and Terminology

1. Vertices (Nodes): These are the individual elements or entities in


the graph. Each vertex can hold data associated with it, known as the
vertex's "payload."

2. Edges: These are the connections or relationships between pairs of


vertices. An edge connects two vertices and may have an optional
weight or cost associated with it, representing some quantitative value
or distance.

3. Path: Path is a sequence of vertices connected by edges, which


allows you to move from one vertex to another in the graph. It
represents a route or a series of steps that you can take to traverse
the graph from a starting vertex to an ending vertex, passing through
intermediate vertices (if any) along the way.

4. Edge Weight: Edge weight refers to a numerical value assigned to


an edge in a weighted graph. Weighted graphs are graphs where
each edge has an associated weight or cost

5. Degree: The degree of a vertex in a graph is a measure of the


number of edges incident to that vertex. In other words, it tells us how
many edges are connected to a specific vertex.
Key points about vertex degree

1. Degree of a Vertex: For an undirected graph, the degree of a


vertex is the count of edges connected to that vertex. In a directed
graph, the degree of a vertex is the sum of its in-degree (incoming
edges) and out-degree (outgoing edges).

2. In-Degree: In a directed graph, the in-degree of a vertex is the


number of incoming edges directed towards that vertex.

3. Out-Degree: In a directed graph, the out-degree of a vertex is the


number of outgoing edges from that vertex.

4. Self-Loop: If there is an edge connecting a vertex to itself


(self-loop), it is counted as one of the incident edges, contributing to
the degree of that vertex.

5. Isolated Vertex: A vertex with a degree of 0 is called an isolated


vertex, meaning it has no edges incident to it.
Types Of Graphs

Graphs can be classified into various types based on their properties


and characteristics. Here are some of the most common types of
graphs:

1. Undirected Graph: In an undirected graph, the edges have no


direction, meaning the connection between vertices is bidirectional. If
there is an edge between vertex A and vertex B, you can traverse it in
both directions, from A to B and from B to A.

2. Directed Graph: In a directed graph, each edge has a direction


associated with it. This means that the relationship between two
vertices is one-way. If there is a directed edge from vertex A to vertex
B, you can only traverse it from A to B and not vice versa.
3. Weighted Graph: A graph in which each edge has an associated
weight or cost, typically representing some numerical value, distance,
or cost.

4. Unweighted Graph: A graph in which all edges have the same


weight, usually considered to be 1. The specific weights are not
significant in this type of graph.

6. Cyclic Graph: A graph that contains at least one cycle, which is a


closed path that starts and ends at the same vertex, passing through
one or more vertices.

7. Acyclic Graph: A graph that does not contain any cycles. It is a


special type of graph with no closed loops.
8. Connected Graph: A graph where there is a path between every
pair of vertices. All vertices are reachable from any other vertex
through a sequence of edges.
9. Disconnected Graph: A graph that contains multiple connected
components, where some vertices are not reachable from others.
Graph Representation

There are two common ways to represent a graph:

1. Adjacency Matrix: In this representation, a 2D matrix is used to


show the connections between nodes. The rows and columns of the
matrix correspond to the nodes in the graph, and the value in the
matrix cell represents whether there is an edge between the nodes or
not. Typically, a 1 indicates an edge, while a 0 indicates no edge.

For directed graphs, the matrix may not be symmetric since the edge
from node i to node j might be different from the edge from node j to
node i.
Space Complexity:

● O(V^2)
● The matrix requires space proportional to the square of the
number of vertices (V). This can be inefficient for sparse graphs
with few edges.

Time Complexity:

● Checking if an edge exists: O(1)


○ Directly access the matrix cell (i, j) to determine if an edge
is present.
● Adding an edge: O(1)
○ Update the matrix cell (i, j) to add an edge.
● Removing an edge: O(1)
○ Update the matrix cell (i, j) to remove an edge.
● Finding all neighbors of a vertex: O(V)
○ Scan through the entire row of the matrix corresponding to
the vertex to list its neighbors.
2. Adjacency List: In this representation, each node in the graph has
a list of its neighboring nodes. It can be implemented using an array or
a dictionary where the keys are the nodes, and the associated values
are lists containing the neighboring nodes.
Space Complexity:

● O(V + E)
● The space used is proportional to the number of vertices (V) plus
the number of edges (E). This is more space-efficient for sparse
graphs.
Time Complexity:

● Checking if an edge exists: O(V) (worst case)


○ Requires scanning the list of neighbors for a vertex to find a
specific edge.
● Adding an edge: O(1)
○ Simply append the edge to the list of neighbors for both
vertices (for undirected graphs).
● Removing an edge: O(d) (where d is the degree of the vertex)
○ Requires scanning the list of neighbors to remove the
edge.
● Finding all neighbors of a vertex: O(d)
○ Directly access and iterate through the list of neighbors for
the vertex.
Graph Traversal Algorithms
Graph traversal algorithms are used to explore and visit all the nodes in a
graph. They are essential for many applications, such as finding paths,
searching for specific nodes, and more. Here, we'll cover two fundamental
traversal methods: Depth-First Search (DFS) and Breadth-First Search
(BFS).

1. Depth-First Search (DFS)


DFS explores as deeply as possible along each branch before
backtracking. This means it starts at a node, explores all its neighbors
before moving to the next node.

Algorithm
1. Start with an initial node as the current node.
2. Mark the current node as visited to keep track of the nodes
already explored.
3. Process the current node (e.g., print its value or perform any
desired operation).
4. Find an unvisited neighboring node of the current node and
make it the new current node.
5. If all neighboring nodes are visited, backtrack to the previous
node (pop it from the stack if using an explicit stack).
6. Repeat steps 2 to 5 until there are no more unvisited nodes.
Time Complexity: O(V + E)

● V is the number of vertices (nodes).


● E is the number of edges. Each vertex and edge is explored
once.

Space Complexity: O(V)

● The space is used by the recursion stack in the worst case,


where V is the number of vertices.

2. Breadth-First Search (BFS)


BFS explores all nodes at the present depth level before moving on to
nodes at the next depth level. It uses a queue to keep track of the
nodes to be explored.

Algorithm -:
1. Pick a starting node, push it into the queue, mark it as visited.
2. In every iteration, pop out the 'x' node and put it in the solution
vector.
3. All the unvisited adjacent nodes from 'x' are pushed into the
queue.
4. Repeat steps 2 and 3.
Time Complexity: O(V + E)

● V is the number of vertices.


● E is the number of edges. Each vertex and edge is processed
once.

Space Complexity: O(V)

● The space is used by the queue and visited list, where V is the
number of vertices.

Applications of Graph Data Structures -:

1. Google Maps: Uses graphs to model transportation systems.


Intersections are vertices, and roads are edges. Algorithms
calculate the shortest path between locations.
2. Facebook: Models users as vertices and friendships as edges.
Friend suggestion algorithms use graph theory. This is an
example of an undirected graph.
3. World Wide Web: Web pages are vertices, and hyperlinks are
directed edges from one page to another. This directed graph
forms the basis of Google's PageRank algorithm.
4. Operating Systems: Uses Resource Allocation Graphs where
processes and resources are vertices. Edges indicate resource
allocation or requests. Cycles in the graph can lead to
deadlocks.
5. Mapping Systems and GPS: Employ graphs to find optimal
locations and routes, helping in navigation and location-based
services.
6. Microsoft Excel: Utilizes Directed Acyclic Graphs (DAGs) to
represent dependencies and perform calculations.
7. Dijkstra’s Algorithm: Uses graphs to find the shortest path
between nodes in weighted graphs.
8. Social Media: Graphs track user interactions, suggest posts,
friends, and recommendations based on connections and
preferences.
9. Biochemical Applications: Graphs model protein structures,
DNA sequences, and other biochemical relationships.

You might also like