0% found this document useful (0 votes)
9 views

Data structure algo Graph Classroom_Discussion notes

The document provides an overview of graphs, including their definitions, classifications, and representations such as adjacency matrices, lists, and edge lists. It discusses graph traversal techniques like Depth-First Search and Breadth-First Search, as well as algorithms for finding Minimum Spanning Trees and shortest paths, specifically Dijkstra's and Bellman-Ford algorithms. The content is aimed at understanding the mathematical structure of graphs and their applications in various fields.

Uploaded by

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

Data structure algo Graph Classroom_Discussion notes

The document provides an overview of graphs, including their definitions, classifications, and representations such as adjacency matrices, lists, and edge lists. It discusses graph traversal techniques like Depth-First Search and Breadth-First Search, as well as algorithms for finding Minimum Spanning Trees and shortest paths, specifically Dijkstra's and Bellman-Ford algorithms. The content is aimed at understanding the mathematical structure of graphs and their applications in various fields.

Uploaded by

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

P a g e | 62

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 can be classified into various types based on different criteria:


 Directed vs. Undirected Graphs: In a directed graph, edges have a direction associated with
them, while in an undirected graph, edges do not have a direction.
 Weighted vs. Unweighted Graphs: In a weighted graph, each edge is associated with a
weight or cost, whereas in an unweighted graph, edges have no associated weight.
 Cyclic vs. Acyclic Graphs: A cyclic graph contains at least one cycle (a path that starts and
ends at the same vertex), while an acyclic graph has no cycles.
 Connected vs. Disconnected Graphs: In a connected graph, there is a path between every
pair of vertices, while in a disconnected graph, there are one or more pairs of vertices between
which there is no path.

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.

Lecture Notes: “Data Structures and Algorithms”,


for more details Contact Dr. Atul Nandwal-9229696960
P a g e | 63

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.

Figure : adjacency matrix


 Adjacency List:
o An adjacency list is a collection of lists or arrays, one for each vertex in the graph.
o Each list contains the vertices adjacent to the corresponding vertex. For weighted
graphs, each entry in the list may include both the adjacent vertex and the weight of
the edge.
o Adjacency lists are efficient for sparse graphs (graphs with few edges) and consume
less memory compared to adjacency matrices.

Figure : adjacency list

Lecture Notes: “Data Structures and Algorithms”,


for more details Contact Dr. Atul Nandwal-9229696960
P a g e | 64

 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.

Lecture Notes: “Data Structures and Algorithms”,


for more details Contact Dr. Atul Nandwal-9229696960
P a g e | 65

Paths and Cycles

Figure : Patha and cycles


 Path:
o A path in a graph is a sequence of non-repeated nodes connected by edges. It allows
you to travel from one node to another.
o Specifically, a path is a graph where the first and last nodes have a degree of one, and
all other intermediate nodes have a degree of two.
o If the graph contains directed edges, a path is often referred to as a dipath. In this
case, all edges must have the same direction.
o Key properties related to paths:
o A graph is weakly connected if there exists an undirected path (ignoring edge
directions in directed graphs) between any two nodes.
o If a directed graph provides the opposite-oriented path for each available path, it is
strongly connected.
o The distance between two nodes in a graph is the length of the shortest path
connecting them (otherwise, the distance is infinity).
 Cycle:
o A cycle is a special type of path that starts and ends at the same node.
o Cycles can be useful in some scenarios, but they often pose challenges in graph
algorithms. Failing to detect a cycle can lead to endless loops in implementations.

Lecture Notes: “Data Structures and Algorithms”,


for more details Contact Dr. Atul Nandwal-9229696960
P a g e | 66

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.

Figure : Graph traversal


Popular techniques for graph traversal:
 Depth-First Search (DFS):
o DFS explores as far as possible along each branch before backtracking.
o It starts at a chosen node and explores as far as possible along each branch before
backtracking.
o DFS can be implemented using recursion or a stack data structure.
o Common applications include cycle detection, topological sorting, and finding
connected components.
 Breadth-First Search (BFS):
o BFS explores all neighbor nodes at the present depth level before moving on to the
nodes at the next depth level.
o It starts at a chosen node and explores all its neighbors before moving on to the
neighbors' neighbors.
o BFS uses a queue data structure to maintain the order of exploration.
o It is often used for finding shortest paths in unweighted graphs and for level-order
traversal in trees.

Lecture Notes: “Data Structures and Algorithms”,


for more details Contact Dr. Atul Nandwal-9229696960
P a g e | 67

 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.

Minimum Spanning Trees


A Minimum Spanning Tree (MST) is a subset of edges in an undirected weighted graph that connects
all the vertices together without forming any cycles and has the minimum possible total edge weight. In
other words, it is a tree (a connected acyclic graph) that spans all the vertices of the original graph while
minimizing the total weight of the edges.

Lecture Notes: “Data Structures and Algorithms”,


for more details Contact Dr. Atul Nandwal-9229696960
P a g e | 68

Figure : Minimum Spanning Tree

Characteristics of Minimum Spanning Trees:


 Spanning: An MST spans all the vertices of the original graph, ensuring that every vertex is
reachable from any other vertex.
 Tree Structure: An MST is a tree, which means it is a connected acyclic graph. It contains no
cycles, so there is exactly one unique path between any pair of vertices.
 Minimum Weight: Among all possible spanning trees of the graph, the MST has the minimum
total weight. The weight of a spanning tree is the sum of the weights of its edges.
Minimum Spanning Trees have various applications in network design, including:
o Communication Networks: MSTs are used to minimize the cost of laying cables or
establishing communication links between different locations while ensuring
connectivity.
o Transportation Networks: In transportation planning, MSTs can represent the most
efficient routes for delivering goods or passengers between various locations.
o Cluster Analysis: MSTs are used in clustering algorithms to identify the most
important connections between data points in a dataset.
o Approximation Algorithms: MSTs are often used as a component in approximation
algorithms for solving optimization problems efficiently.

Minimum Spanning Tree of a Graph


 Kruskal's Algorithm:
Kruskal's algorithm is a greedy algorithm that starts with an empty set of edges
and repeatedly adds the lightest edge that doesn't create a cycle until all
vertices are connected.
 Prim's Algorithm:
Prim's algorithm is also a greedy algorithm that grows the MST from an
arbitrary vertex, adding the shortest edge that connects a vertex in the MST to
a vertex outside of it until all vertices are included in the tree.

Lecture Notes: “Data Structures and Algorithms”,


for more details Contact Dr. Atul Nandwal-9229696960
P a g e | 69

Minimum Spanning Trees formation


To find Minimum Spanning Trees (MSTs) in a graph, there are two primary algorithms:
Kruskal's algorithm and Prim's algorithm. Both algorithms are greedy and aim to construct the
MST by iteratively selecting edges that minimize the total weight while ensuring connectivity.
 Kruskal's Algorithm:
o Initialize an empty set to represent the MST.
o Sort all the edges in non-decreasing order of their weights.
o Iterate through the sorted edges and add each edge to the MST if adding it does not
create a cycle (i.e., if it does not connect two vertices that are already connected).
o Continue this process until all vertices are connected, and the MST is formed.
 Prim's Algorithm:
o Choose an arbitrary vertex to start the MST.
o Initialize a priority queue (min-heap) to store candidate edges, initially containing all
edges connected to the chosen vertex.
o While there are vertices not yet included in the MST:
o Extract the minimum-weight edge from the priority queue.
o If the edge connects a vertex not yet in the MST, add it to the MST and add all edges
connected to this new vertex to the priority queue.
o Repeat until all vertices are included in the MST.

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.

Procedure to find Shortest Path?


To find the shortest path between two vertices in a graph, various algorithms can be employed, with two
of the most popular being Dijkstra's algorithm and the Bellman-Ford algorithm. Both algorithms aim to
determine the shortest path from a source vertex to all other vertices in the graph.
Overview of how these algorithms work:

Lecture Notes: “Data Structures and Algorithms”,


for more details Contact Dr. Atul Nandwal-9229696960
P a g e | 70

 Dijkstra's Algorithm:

Figure: Dijkstra’s Algorithm


o Initialize the distance to the source vertex as 0 and the distance to all other vertices as
infinity.
o Initialize an empty set to keep track of visited vertices and a priority queue (min-heap)
to store vertices based on their tentative distances from the source.
o While the priority queue is not empty:
o Extract the vertex with the smallest tentative distance from the priority queue.
o For each neighbor of the extracted vertex:
o Calculate the tentative distance from the source to the neighbor through the extracted
vertex.
o If this distance is smaller than the current distance recorded for the neighbor, update
the distance.
o Add the neighbor to the priority queue if it hasn't been visited yet.
o Once all vertices have been visited or the destination vertex has been reached, the
shortest path and its distance are known.

 Bellman-Ford Algorithm:

Figure : Bellman-ford Algorithm


o Initialize the distance to the source vertex as 0 and the distance to all other vertices as
infinity.

Lecture Notes: “Data Structures and Algorithms”,


for more details Contact Dr. Atul Nandwal-9229696960
P a g e | 71

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.

Lecture Notes: “Data Structures and Algorithms”,


for more details Contact Dr. Atul Nandwal-9229696960

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy