Data structure algo Graph Classroom_Discussion notes
Data structure algo Graph Classroom_Discussion notes
Graphs:
A graph is a mathematical structure that consists of a set of vertices (also known as nodes or
points) and a set of edges (also known as arcs or lines) that connect pairs of vertices. Graphs
are used to represent relationships between objects.
Figure : Graph
Formally, a graph G is defined as an ordered pair G = (V, E) , where:
o V is a set of vertices or nodes.
o E is a set of edges, where each edge is a pair (u, v) that indicates a connection
between vertex u and vertex v . Edges can be directed or undirected, depending on
whether they have a specified direction or not.
Graphs are used in various fields such as computer science, mathematics, social sciences, and
transportation networks. They provide a versatile and powerful way to model and analyze relationships
between entities. Graph theory, the branch of mathematics concerned with the study of graphs, has
applications in numerous areas, including network analysis, optimization, and data science.
Representation of Graph
There are several ways to represent a graph, each with its own advantages and suitable applications.
The most common representations include:
Adjacency Matrix:
o An adjacency matrix is a 2D array of size |V| times |V|, where |V| is the number of
vertices in the graph.
o Each entry a_{ij} in the matrix represents whether there is an edge between vertex i
and vertex j. For unweighted graphs, a_{ij} is typically either 1 (indicating the presence
of an edge) or 0 (indicating the absence of an edge). For weighted graphs, a_{ij} may
represent the weight of the edge.
o Adjacency matrices are efficient for dense graphs (graphs with many edges) and
support constant time lookup to determine whether there is an edge between two
vertices.
Edge List:
o An edge list is a simple list of all the edges in the graph.
o Each entry in the list represents an edge, typically as a tuple or pair of vertices. For
weighted graphs, the entry may include the weight of the edge as well.
o Edge lists are straightforward and memory-efficient, but they are less efficient for
certain operations like determining adjacency between vertices.
Incidence Matrix:
o An incidence matrix is similar to an adjacency matrix but represents both vertices and
edges.
o Rows correspond to vertices, and columns correspond to edges. Each entry a_{ij}
indicates whether vertex i is incident to edge j.
o Incidence matrices are primarily used in certain graph algorithms and network analysis.
The choice of representation depends on factors such as the density of the graph, the operations to be
performed on the graph, and memory constraints. In practice, different representations may be used
interchangeably based on the specific requirements of the problem at hand.
In summary, paths allow traversal between nodes, while cycles form closed loops within a
graph. Understanding these concepts is crucial for working with graphs in various applications.
Graph Traversal
Graph traversal refers to the systematic exploration of all nodes in a graph data structure. In a graph,
nodes (vertices) are connected by edges, forming a network of relationships. Traversal algorithms aim
to visit each node exactly once, discovering the structure and properties of the graph.
Dijkstra's Algorithm:
o Dijkstra's algorithm is used to find the shortest paths from a single source node to all
other nodes in a weighted graph with non-negative edge weights.
o It maintains a set of nodes whose shortest distance from the source node is known and
continually expands this set.
o Dijkstra's algorithm uses a priority queue to select the node with the shortest distance
to the source for expansion.
o It is widely used in network routing and pathfinding applications.
A* Search Algorithm:
o A* search is an informed search algorithm that finds the shortest path from a start node
to a goal node by considering both the actual cost from the start node and an estimate
of the remaining cost to the goal node.
o It uses a heuristic function to estimate the remaining cost, guiding the search towards
the goal efficiently.
o A* search combines the benefits of both BFS (completeness) and DFS (space
efficiency) while also leveraging the heuristic information.
o It is commonly used in pathfinding problems, such as in route planning and video game
AI.
Both Kruskal's and Prim's algorithms guarantee to find the MST of a graph. The choice between them
may depend on factors such as the structure of the graph, the availability of edge weights, and
implementation considerations.
These algorithms have a time complexity of O(E log E) for dense graphs and O(E log V) for sparse
graphs, where E is the number of edges and V is the number of vertices in the graph. Kruskal's
algorithm is often preferred for sparse graphs, while Prim's algorithm may be more efficient for dense
graphs.
Dijkstra's Algorithm:
Bellman-Ford Algorithm:
o Relax all edges repeatedly (|V| - 1) times, where |V| is the number of vertices in the
graph. In each relaxation step, update the distances based on the current shortest path
information.
o After |V| - 1 iterations, all shortest paths that contain at most |V| - 1 edges have been
found.
o Check for negative-weight cycles: Perform one more iteration and if any distance
changes, there exists a negative-weight cycle in the graph.
o If no negative-weight cycles are detected, the shortest path and its distance can be
determined.
These algorithms guarantee to find the shortest path in a graph with non-negative edge
weights. Dijkstra's algorithm is typically faster for graphs with positive edge weights and is more
commonly used when only a single-source shortest path is required. Bellman-Ford's algorithm
is more versatile as it can handle graphs with negative edge weights and detect negative-
weight cycles, but it is generally slower.