0% found this document useful (0 votes)
137 views8 pages

Graph and Tree Algorithms Overview

The document provides an overview of graph and tree algorithms essential for the Design and Analysis of Algorithms course, covering key concepts such as Depth-First Search (DFS), Breadth-First Search (BFS), and various shortest path algorithms like Dijkstra's and Bellman-Ford. It also discusses minimum spanning tree algorithms, tree traversal methods, and network flow algorithms, highlighting their applications and differences. The content serves as a foundational guide for understanding algorithmic approaches to solving problems related to networks and hierarchical data structures.

Uploaded by

Ankush Maity
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)
137 views8 pages

Graph and Tree Algorithms Overview

The document provides an overview of graph and tree algorithms essential for the Design and Analysis of Algorithms course, covering key concepts such as Depth-First Search (DFS), Breadth-First Search (BFS), and various shortest path algorithms like Dijkstra's and Bellman-Ford. It also discusses minimum spanning tree algorithms, tree traversal methods, and network flow algorithms, highlighting their applications and differences. The content serves as a foundational guide for understanding algorithmic approaches to solving problems related to networks and hierarchical data structures.

Uploaded by

Ankush Maity
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

DESIGN AND ANALYSIS OF ALGORITHM MODULE 3/UNIT 3

Btech CSE 2nd yr


Tathagata sir

Graph and Tree algorithms are foundational elements in the study of Design and Analysis of Algorithms
(DAA), offering systematic approaches for solving problems related to networks, connectivity, optimization,
and hierarchical data structures. Here's an overview of key algorithms in these categories:

Graph Algorithms

Graphs are versatile data structures that model a set of objects (vertices or nodes) and their interconnections
(edges). Algorithms that operate on graphs can be used for tasks like searching, pathfinding, and analyzing
network structures.

1. Depth-First Search (DFS): An algorithm for traversing or searching tree or graph data structures. It starts at a
selected node and explores as far as possible along each branch before backtracking.

2. Breadth-First Search (BFS): Another fundamental search algorithm used to explore nodes and vertices of a
graph. It starts at a selected node and explores all of the neighbor nodes at the present depth prior to moving on
to the nodes at the next depth level.

3. Dijkstra's Algorithm: An algorithm for finding the shortest paths between nodes in a graph, which may
represent, for example, road networks.

4. Bellman-Ford Algorithm: Solves the shortest path problem for a graph with negative edge weights, detecting
negative cycles.

5. Floyd-Warshall Algorithm: A dynamic programming algorithm to find the shortest paths between all pairs of
vertices in a weighted graph.

6. Kruskal's Algorithm: A minimum spanning tree algorithm that finds an edge of the least possible weight that
connects any two trees in the forest.

7. Prim's Algorithm: Another minimum spanning tree algorithm that builds the tree one vertex at a time, from
an arbitrary starting vertex, by adding the cheapest possible connection from the tree to another vertex.

Tree Algorithms

Trees are a special type of graph designed to represent hierarchical structures without cycles, offering efficient
algorithms for data storage and retrieval.

1. Tree Traversals (Inorder, Preorder, Postorder): Techniques to visit all the nodes of a tree systematically.

2. Binary Search Tree Operations: Include searching, insertion, deletion, and traversal in a binary search tree, a
tree data structure that speeds up the process of finding, adding, and removing values.

BTECH/BSC/BCA/MCA tathagatamtech13@[Link]/tutoringassign@[Link]
DESIGN AND ANALYSIS OF ALGORITHM MODULE 3/UNIT 3
Btech CSE 2nd yr
Tathagata sir

3. Balancing Trees: Algorithms like AVL and Red-Black Tree are designed to ensure that the tree remains
balanced, thereby guaranteeing that operations like search, insert, and delete take logarithmic time.

4. Segment Tree: A tree algorithm that allows for fast updates and sum calculations over array intervals, useful
in scenarios requiring frequent updates and queries over a range of data points.

5. Fenwick Tree (Binary Indexed Tree): Provides a way to represent an array of values in a form that supports
prefix sum queries and updates efficiently, suitable for calculating cumulative frequency tables or implementing
the arithmetic coding algorithm.

Depth First Search (DFS)

Depth First Search (DFS) explores a graph or tree by moving as far as possible along each branch before
backtracking. This means it explores the depth of any particular path before exploring its breadth. A stack
(which can be the call stack via recursion, or an explicit stack data structure) is used to keep track of the nodes
being explored.

How DFS Works:

1. Start at a selected node (often the root in a tree, or any arbitrary node in a graph).
2. Visit the starting node and mark it as explored.
3. For each adjacent unvisited node, start a recursive visit from that node. Essentially, you "go deep" before you
"go wide."
4. Backtrack as necessary, meaning once you reach a node with no unvisited adjacent nodes, return to its parent
node and follow the next adjacent node until all nodes are visited.

Applications of DFS:

Finding connected components in an undirected graph.


Topological sorting in a directed graph.
Detecting cycles in a graph.
Pathfinding algorithms and puzzle solving.

Breadth First Search (BFS)

Breadth First Search (BFS) explores the graph or tree level by level, starting from the selected node (often the
root in a tree). It explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the
next depth level. A queue is used to keep track of the nodes to be explored.

BTECH/BSC/BCA/MCA tathagatamtech13@[Link]/tutoringassign@[Link]
DESIGN AND ANALYSIS OF ALGORITHM MODULE 3/UNIT 3
Btech CSE 2nd yr
Tathagata sir

How BFS Works:

1. Start at a selected node.


2. Visit the starting node and mark it as explored.
3. Queue up all adjacent and unvisited nodes and mark them as visited.
4. Dequeue a node from the queue to explore it, then queue up its adjacent unvisited nodes.
5. Repeat steps 3 and 4 until the queue is empty, meaning all nodes reachable from the starting node have been
explored.

Applications of BFS:

Finding the shortest path on unweighted graphs.


Level-order traversal of trees.
Finding connected components in an undirected graph.
Algorithms like finding the minimum spanning tree (in an unweighted graph).

Difference between DFS and BFS

Depth First Search (DFS) and Breadth First Search (BFS) are two cornerstone algorithms for graph traversal,
each with distinct characteristics and applications. Understanding their differences is crucial for selecting the
appropriate algorithm based on the specific requirements of the problem at hand. Here's a concise comparison:

Strategy

DFS explores as far as possible along each branch before backtracking. This means it goes deep into the graph
as much as possible until it hits a node with no unvisited neighbors, then it backtracks and explores other
branches.
BFS explores all the neighbor nodes at the current depth level before moving to the nodes at the next depth
level. This means it goes wide, exploring all direct neighbors before going deeper in the graph.

Data Structure Used

DFS typically uses a stack to keep track of the vertices to visit next. This stack can be either an explicit data
structure or the call stack used implicitly with recursion.
BFS uses a queue to keep track of the next vertex to visit. By enqueuing vertices level by level, BFS ensures a
breadth-wise traversal.

Applications

DFS is particularly useful for tasks like topological sorting, solving puzzles that require exploring all possible
paths (like a maze), and detecting cycles in a graph.

BTECH/BSC/BCA/MCA tathagatamtech13@[Link]/tutoringassign@[Link]
DESIGN AND ANALYSIS OF ALGORITHM MODULE 3/UNIT 3
Btech CSE 2nd yr
Tathagata sir

BFS is used to find the shortest path on unweighted graphs, level order traversal of trees, and in algorithms for
finding the minimum spanning tree in unweighted graphs.

Performance

In terms of time complexity, both DFS and BFS are O(V + E) for a graph represented using an adjacency list,
where V is the number of vertices and E is the number of edges. This means they are both linear in the size of
the graph.
The space complexity can vary depending on the graph's structure and the algorithm's implementation. DFS can
be more space-efficient in terms of the maximum size of the stack (especially if implemented recursively with
tail recursion optimization). BFS, however, might require more space to store all the vertices at the current and
next levels in the queue.

Path Finding

DFS might not always find the shortest path in a graph because it dives deep into the graph, possibly taking a
longer route to reach a goal node.
BFS is guaranteed to find the shortest path in an unweighted graph because it explores all neighbors at the
current depth before moving on, effectively finding the path that involves the least number of edges.

Use Cases

DFS is often preferred in scenarios where we need to explore all possible solutions and when the graph has
large depths and relatively fewer branches.
BFS is the go-to algorithm when the solution is not far from the root node, in scenarios where paths have the
same weight, or when finding the shortest path is essential.

Shortest Path Algorithms

Shortest path algorithms find the shortest path or minimum distance between two vertices in a graph, which
may represent road networks, communication infrastructures, or any other kind of weighted network.

Dijkstra's Algorithm: Finds the shortest paths from a single source vertex to all other vertices in a graph with
non-negative edge weights. It's efficient but does not work with graphs containing negative weight edges.

Bellman-Ford Algorithm: Capable of handling graphs with negative weight edges, this algorithm can also detect
negative weight cycles. It finds the shortest paths from a single source vertex to all other vertices.

Floyd-Warshall Algorithm: A dynamic programming approach to find shortest paths between all pairs of
vertices in a weighted graph, including those with negative weights. It's very useful for dense graphs.

BTECH/BSC/BCA/MCA tathagatamtech13@[Link]/tutoringassign@[Link]
DESIGN AND ANALYSIS OF ALGORITHM MODULE 3/UNIT 3
Btech CSE 2nd yr
Tathagata sir

Transitive Closure

Transitive closure of a graph is a way to determine reachability. It tells us whether there's a path from every
vertex to every other vertex in the graph. The Floyd-Warshall algorithm is commonly used for computing the
transitive closure of a graph, providing a way to quickly query the reachability of vertices.

Minimum Spanning Tree (MST)

An MST of a graph is a subset of the edges that connect all vertices together, without any cycles and with the
minimum possible total edge weight. MSTs are crucial for minimizing costs in network design problems.

Kruskal's Algorithm: A greedy algorithm that sorts all the edges of the graph by their weight and adds them one
by one to the MST if they don't form a cycle.

Prim's Algorithm: Starting with a single vertex, Prim's algorithm adds edges to the MST from the original
graph, connecting a vertex inside the tree to a vertex outside the tree at the lowest possible cost.

Topological Sorting

Topological sorting is the linear ordering of vertices in a directed acyclic graph (DAG), such that for every
directed edge uv from vertex u to vertex v, u comes before v in the ordering. This is useful for scheduling tasks,
ordering cells in spreadsheets, and resolving symbol dependencies in compilers, among others.

Network Flow Algorithms

Network flow algorithms solve problems related to the flow of resources through a network, optimizing the
distribution of resources from sources to sinks.

Ford-Fulkerson Algorithm: Computes the maximum flow in a flow network. It identifies paths through which
more flow can be sent and increases the flow until no more augmenting paths are found.

Edmonds-Karp Algorithm: An implementation of the Ford-Fulkerson method that uses BFS for finding the
shortest path (in terms of the number of edges) as the augmenting path, which ensures polynomial time
complexity.

BTECH/BSC/BCA/MCA tathagatamtech13@[Link]/tutoringassign@[Link]
DESIGN AND ANALYSIS OF ALGORITHM MODULE 3/UNIT 3
Btech CSE 2nd yr
Tathagata sir

Algorithms in details:

Dijkstra's Algorithm

Purpose: Finds the shortest paths from a single source vertex to all other vertices in a graph with non-negative
edge weights.

Example:
Imagine a network of roads connecting cities, where edges represent roads and weights represent distances.
Starting from City A, Dijkstra's algorithm calculates the shortest path to every other city in the network.

1. Initialize the distance to all nodes as infinity, except for the starting node, which is set to zero.
2. Set the current node as the starting node. Mark all nodes as unvisited.
3. For the current node, consider all its unvisited neighbors. Calculate the distance from the starting node to
each neighbor. If the calculated distance of a node is less than the known distance, update the shortest distance.
4. Mark the current node as visited. A visited node will not be checked again.
5. Select the unvisited node with the smallest distance from the start node as the next "current node" and repeat
steps 3-4.
6. Continue the process until all nodes have been visited.

Bellman-Ford Algorithm

Purpose: Capable of handling graphs with negative weight edges, it finds the shortest paths from a single source
vertex to all other vertices and can also detect negative weight cycles.

Example:
Consider a financial network where edges represent currency exchange rates (which can be negative due to
transaction costs or arbitrage opportunities) between currencies. Starting from one currency, the Bellman-Ford
algorithm can find the most profitable exchanges to any other currency.

1. Initialize distances from the source to all vertices as infinite and distance to the source itself as 0.
2. For each edge (u, v) in the graph, if the distance to the destination v can be shortened by taking the edge (u,
v), update the distance to v.
3. Repeat step 2 for |V| 1 iterations, where |V| is the number of vertices in the graph.
4. Check for negative weight cycles by repeating step 2. If a value changes, then a negative cycle exists in the
graph.

Floyd-Warshall Algorithm

Purpose: A dynamic programming approach to find shortest paths between all pairs of vertices in a weighted
graph, including those with negative weights.

BTECH/BSC/BCA/MCA tathagatamtech13@[Link]/tutoringassign@[Link]
DESIGN AND ANALYSIS OF ALGORITHM MODULE 3/UNIT 3
Btech CSE 2nd yr
Tathagata sir

Example:
In a school's classroom network, where paths represent the possibility of direct communication between classes
and weights represent the effectiveness of communication, the Floyd-Warshall algorithm can identify the most
effective communication path between any two classrooms.

1. Initialize a matrix distances where distance[i][j] is the direct distance from i to j (if there is no direct path, it's
infinity, except distance[i][i] = 0).
2. For each vertex k, we try to update every distance[i][j] as min(distance[i][j], distance[i][k] + distance[k][j]).
3. Repeat this process for each vertex k. The final matrix represents the shortest distances between all pairs of
vertices.

Ford-Fulkerson Algorithm

Purpose: Computes the maximum flow in a flow network.

Example:
Imagine a network of water pipes connecting reservoirs (source) to cities (sinks). Edges represent pipes, and
weights represent the capacity of each pipe. The Ford-Fulkerson algorithm identifies the maximum amount of
water that can be delivered from the reservoirs to the cities.

1. Initialize flow in all edges to 0.


2. While there is a path from the source to the sink, with available capacity on all edges in the path (an
augmenting path):
Find the minimum capacity (bottleneck) along the path.
Increase the flow on that path by the bottleneck value.
3. The algorithm terminates when no more augmenting paths are found. The maximum flow is the total flow
reaching the sink.

Edmonds-Karp Algorithm

Purpose: An implementation of the Ford-Fulkerson method that uses BFS for finding the shortest path (in terms
of the number of edges) as the augmenting path.

Example:
Using the same network of water pipes as above, the Edmonds-Karp algorithm specifically uses BFS to find the
shortest path (least number of edges) from the source to the sink that can carry more water.

1. Similar to Ford-Fulkerson, initialize flow in all edges to 0.


2. Use BFS to find the shortest augmenting path from the source to the sink.

BTECH/BSC/BCA/MCA tathagatamtech13@[Link]/tutoringassign@[Link]
DESIGN AND ANALYSIS OF ALGORITHM MODULE 3/UNIT 3
Btech CSE 2nd yr
Tathagata sir

BFS ensures that the path with the minimum number of edges is chosen, which helps in avoiding infinite
loops in the presence of cycles and ensures polynomial time complexity.
3. Follow the Ford-Fulkerson steps to increase the flow along the path found by BFS.
4. The algorithm terminates when no augmenting path can be found by BFS, ensuring that the maximum flow
has been reached.

Each of these algorithms addresses specific problems in graph theory, from finding the shortest paths to
maximizing flow in networks, showcasing their utility in solving complex computational and real-world
problems.

BTECH/BSC/BCA/MCA tathagatamtech13@[Link]/tutoringassign@[Link]

You might also like