DAA Unit2
DAA Unit2
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.
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) 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.
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.
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.
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: