0% found this document useful (0 votes)
10 views53 pages

DAA Unit2

Uploaded by

deepthi.it
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)
10 views53 pages

DAA Unit2

Uploaded by

deepthi.it
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/ 53

UNIT-II

Graphs: breadth first search, depth first search, spanning trees, connected and bi
connected components.
Greedy method: General method, applications-Job sequencing with deadlines, 0/1
knapsack problem, Minimum cost spanning trees, Single source shortest path problem.

Graph:
Graph is a non-linear data structure consisting of vertices and edges. The vertices are
sometimes also referred to as nodes and the edges are lines or arcs that connect any two
nodes in the graph. More formally a Graph is composed of a set of vertices( V ) and a set of
edges( E ). The graph is denoted by G(V, E).
Graphs useful in fields such as social network analysis, recommendation systems, and
computer networks.
Components of a Graph:
 Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are also
known as vertex or nodes. Every node/vertex can be labeled or unlabeled.
 Edges: Edges are drawn or used to connect two nodes of the graph. It can be ordered
pair of nodes in a directed graph. Edges can connect any two nodes in any possible way.
There are no rules. Every edge can be labelled/unlabelled.

Basic Operations on Graphs:


Below are the basic operations on the graph:
 Insertion of Nodes/Edges in the graph – Insert a node into the graph.
 Deletion of Nodes/Edges in the graph – Delete a node from the graph.
 Searching on Graphs – Search an entity in the graph.
 Traversal of Graphs – Traversing all the nodes in the graph.

Applications of Graph:
Following are the real-life applications:
 Graphs are used to represent social networks, such as networks of friends on social
media.
 Graphs can be used to represent the topology of computer networks, such as the
connections between routers and switches.
 Graphs are used to represent the connections between different places in a
transportation network, such as roads and airports.
 Graphs are used in Neural Networks where vertices represent neurons and edges
represent the synapses between them.
Types of Graphs
 Undirected Graphs: A graph in which edges have no direction.
 Directed Graphs: A graph in which edges have a direction, i.e., the edges have
arrows indicating the direction of traversal.
 Weighted Graphs: A graph in which edges have weights or costs associated with
them.
 Trees: A connected graph with no cycles.
Advantages of graphs:
1. Graphs can be used to model and analyze complex systems and relationships.
2. They are useful for visualizing and understanding data.
3. Graph algorithms are widely used in computer science and other fields, such as social
network analysis, logistics, and transportation.
4. Graphs can be used to represent a wide range of data types, including social networks,
road networks, and the internet.
Disadvantages of graphs:
1. Large graphs can be difficult to visualize and analyze.
2. Graph algorithms can be computationally expensive, especially for large graphs.

Breadth First Search (BFS) for a Graph:

 Breadth First Search (BFS) is a graph traversal algorithm that explores all the
vertices in a graph at the current depth before moving on to the vertices at the next
depth level.
 It starts at a specified vertex and visits all its neighbors before moving on to the next
level of neighbors.
 BFS is commonly used in algorithms for pathfinding, connected components, and
shortest path problems in graphs.

Breadth First Search (BFS) for a Graph Algorithm:

1. Initialization: Insert the starting node into a queue and mark it as visited.
2. Exploration: While the queue is not empty:
 Delete a node from the queue and visit it (e.g., print its value).
 For each unvisited neighbor of the deleted node:
o Insert the neighbor into the queue.
o Mark the neighbor as visited.
3. Termination: Repeat step 2 until the queue is empty.

How Does the BFS Algorithm Work?


 Starting from the root, all the nodes at a particular level are visited first and then
the nodes of the next level are traversed till all the nodes are visited.
 To do this a queue is used.
 All the adjacent unvisited nodes of the current level are inserted into the queue and
the nodes of the current level are marked visited and popped from the queue.

Example:
Time Complexity of BFS Algorithm: O(V + E)
BFS explores all the vertices and edges in the graph. In the worst case, it visits every vertex
and edge once. Therefore, the time complexity of BFS is O(V + E), where V and E are the
number of vertices and edges in the given graph.

Space Complexity of BFS Algorithm: O(V)

Applications of Breadth First Search:


 Shortest Path and Minimum Spanning Tree for unweighted graph: In an
unweighted graph, the shortest path is the path with the least number of edges. With
Breadth First, we always reach a vertex from a given source using the minimum
number of edges. Also, in the case of unweighted graphs, any spanning tree is
Minimum Spanning Tree and we can use either Depth or Breadth first traversal for
finding a spanning tree.
 Minimum Spanning Tree for weighted graphs: We can also find Minimum
Spanning Tree for weighted graphs using BFT, but the condition is that the weight
should be non-negative and the same for each pair of vertices.
 GPS Navigation systems: Breadth First Search is used to find all neighboring
locations.
 Broadcasting in Network: In networks, a broadcasted packet follows Breadth First
Search to reach all nodes.

Depth First Search or DFS for a Graph


 Depth First Traversal (or DFS) for a graph is similar to Depth First Traversal of a
tree.
 The only catch here is, that, unlike trees, graphs may contain cycles (a node may be
visited twice).
 To avoid processing a node more than once, use a boolean visited array.
 A graph can have more than one DFS traversal.

How does DFS Algorithm work?


 Depth-first search is an algorithm for traversing or searching tree or graph data
structures.
 To do this a stack is used.
 The algorithm starts at the root node (selecting some arbitrary node as the root
node in the case of a graph) and pushed onto the stack and marked as visited.
 Considering the topmost node as current node repeat above step till all possible
nodes along each branch before backtracking.
 Pop the node if there are no further nodes to be pushed.
 Repeat the same until stack becomes empty.
Time complexity: O(V + E), where V is the number of vertices and E is the number of edges
in the graph.

Applications of Depth First Search:


 Detecting cycle in a graph: A graph has a cycle if and only if we see a back edge
during DFS. So we can run DFS for the graph and check for back edges.
 Path Finding: We can specialize the DFS algorithm to find a path between two given
vertices.
 Finding Strongly Connected Components of a graph: A directed graph is called
strongly connected if there is a path from each vertex in the graph to every other
vertex.
 Maze generation: Depth-first search can be used to generate random mazes.

Spanning Tree

 A spanning tree is a subset of Graph G, such that all the vertices are connected
using minimum possible number of edges.
 A spanning tree does not have cycles and a graph may have more than one
spanning tree.
Properties of a Spanning Tree:

 A Spanning tree does not exist for a disconnected graph.


 For a connected graph having N vertices then the number of edges in the
spanning tree for that graph will be N-1.
 A Spanning tree does not have any cycle.
 We can construct a spanning tree for a complete graph by removing E-
N+1 edges, where E is the number of Edges and N is the number of vertices.
 It states that the number of spanning trees in a complete graph with N
vertices is NN-2.

Applications of A Spanning Tree:


 Several path finding algorithms, such as Dijkstra’s algorithm and A* search algorithm,
internally build a spanning tree as an intermediate step.
 Building Telecommunication Network.
 Image Segmentation to break an image into distinguishable components.
 Computer Network Routing Protocol

You might also like