Unit 3 (Non-Linear Data Structures)
Unit 3 (Non-Linear Data Structures)
Structures
3. Parent: The immediate predecessor of a node is called its 11. Subtree: A subtree is a smaller tree that is formed by
parent. It is the node directly above a particular node. selecting a node and all its descendants. Each node in a
tree, excluding the root, can be considered as the root of
4. Child: The nodes directly below a specific node are called its its own subtree.
children. A node can have multiple children.
12. Parental Relationship: The relationship between a
5. Sibling: Nodes that share the same parent are called siblings. parent node and its child nodes is called the parental
They are on the same level of the tree. relationship.
Introduction to trees cont..
Components of a Graph
A finite set of vertices also called nodes.
A finite set of ordered pair of the form (u, v) called
edge. The pair is ordered because (u, v) is not the same
as (v, u) in the case of a directed graph(di-graph). The
pair of the form (u, v) indicates that there is an edge
from vertex u to vertex v. The edges may contain
weight/value/cost.
Directed Graph (Digraph): A directed graph
is a graph where the edges have a specific
Types of graphs direction. The edges can be traversed only in
the direction specified by the edge. Example: A
There are several types of graphs in graph theory, each web page graph, where web pages are
with its own unique characteristics. Here are some represented as vertices, and links between web
common types of graphs: pages are represented as directed edges.
Undirected Graph: An undirected graph is a graph
where the edges have no specific direction. The edges
can be traversed in both directions. Example: A social
network graph, where users are represented as vertices,
and friendships between users are represented as
undirected edges.
Complete Graph: A complete graph is a
graph where there is an edge between every
Types of graphs cont.. pair of distinct vertices. In other words, all
vertices are directly connected to each other.
Weighted Graph: A weighted graph is a graph where Example: A round tournament graph, where
each edge has a numerical weight or value associated participants in a tournament are represented as
with it. These weights can represent distances, costs, or vertices, and there is an edge between every
any other relevant quantity. Example: A transportation pair of participants.
network graph, where cities are represented as vertices,
and the edges are weighted by the distance between
cities.
Acyclic Graph: An acyclic graph is a graph
that does not contain any cycles.
Types of graphs cont..
Cyclic Graph: A cyclic graph is a graph that contains
at least one cycle, which is a path that starts and ends
at the same vertex. Example: A chemical compound
graph, where atoms are represented as vertices, and
there are edges between atoms based on their chemical
bonds.
Types of graphs cont..
Transportation Networks: Graphs are used to model Computer Networks: Graphs model network
transportation systems, such as roads, railways, and topologies and connectivity between devices.
flight routes. Graph algorithms can optimize routes, Graph algorithms assist in routing, network flow
calculate distances, determine the shortest path, and optimization, identifying bottlenecks, and
analyzing network performance.
analyze traffic flow.
Representations of Graphs
Step2: Push node 0 into queue and mark it visited. Step 4: Remove node 1 from the front of queue
and visit the unvisited neighbors and push them
into queue.
Illustration of Breadth-First
Search (BFS) Algorithm cont.. Steps 7: Remove node 4 from the front of
queue and visit the unvisited neighbors and
Step 5: Remove node 2 from the front of queue and visit push them into queue. As we can see that every
the unvisited neighbors and push them into queue. neighbors of node 4 are visited, so move to the
next node that is in the front of the queue.
Depth-first search (DFS) is a graph traversal algorithm • Step 2: Choose a starting node.
that explores a graph by visiting as far as possible • Step 3: Create an empty stack and push the starting
along each branch before backtracking. It starts at a node onto the stack.
specific node (often called the "source" or "start" node) • Step 4: Mark the starting node as visited.
and explores as deep as possible before backtracking
and visiting the next unvisited neighbor. • Step 5: While the stack is not empty, do the following:
• Pop a node from the stack.
The DFS algorithm can be implemented using a stack
• Process or perform any necessary operations on
data structure. the popped node.
• Get all the adjacent neighbors of the popped
node.
• For each adjacent neighbor, if it has not been
visited, do the following:
• Mark the neighbor as visited.
• Push the neighbor onto the stack.
• Step 6: Repeat step 5 until the stack is empty.
Illustration of Depth First
Search(DFS) Step 3: Now, Node 1 at the top of the stack,
so visit node 1 and pop it from the stack and
Step1: Initially stack and visited arrays are empty. put all of its adjacent nodes which are not
visited in the stack.
Step 2: Visit 0 and put its adjacent nodes which are not
Step 4: Now, Node 2 at the top of the stack,
visited yet into the stack.
so visit node 2 and pop it from the stack and
put all of its adjacent nodes which are not
visited (i.e, 3, 4) in the stack.
Illustration of Depth First
Search(DFS) cont.. The time complexity of depth-first search
(DFS) for a graph depends on the representation
Step 5: Now, Node 4 at the top of the stack, so visit of the graph and the specific implementation of
the algorithm.
node 4 and pop it from the stack and put all of its
If the graph is represented using an
adjacent nodes which are not visited in the stack.
adjacency matrix, the time complexity of
DFS is O(V^2), where V is the number of
vertices. This is because, in each step, we
need to iterate through the entire row of the
adjacency matrix to find the neighbors of a
given vertex.
Step 6: Now, Node 3 at the top of the stack, so visit If the graph is represented using an
node 3 and pop it from the stack and put all of its adjacency list, the time complexity of DFS
adjacent nodes which are not visited in the stack. is O(V + E), where V is the number of
vertices and E is the number of edges. This
is because, in each step, we need to iterate
through the adjacency list of a vertex to find
its neighbors.
How to find Shortest Paths in a graph Follow the steps below to solve the problem:
using Dijkstra’s Algorithm • Create a set sptSet (shortest path tree set) that keeps
track of vertices included in the shortest path tree,
i.e., whose minimum distance from the source is
Given a graph and a source vertex in the graph, find calculated and finalized. Initially, this set is empty.
the shortest paths from the source to all vertices in the
• Assign a distance value to all vertices in the input
given graph.
graph. Initialize all distance values as INFINITE.
Examples: given below graph, find the shortest path Assign the distance value as 0 for the source vertex
take 0 as source vertex: so that it is picked first.
• While sptSet doesn’t include all vertices
• Pick a vertex u that is not there in sptSet and has a
minimum distance value.
• Include u to sptSet.
• Then update the distance value of all adjacent
vertices of u.
• To update the distance values, iterate through all
adjacent vertices.
• For every adjacent vertex v, if the sum of the
distance value of u (from source) and weight of
edge u-v, is less than the distance value of v, then
update the distance value of v.
Illustration of How to find Shortest
Paths in a graph using Dijkstra’s
Algorithm
Step 1:
The set sptSet is initially empty and distances assigned
to vertices are {0, INF, INF, INF, INF, INF, INF, INF}
where INF indicates infinite.
Now pick the vertex with a minimum distance value.
The vertex 0 is picked, include it in sptSet.
So sptSet becomes {0}. After including 0 to sptSet,
update distance values of its adjacent vertices.
Adjacent vertices of 0 are 1 and 7. The distance values of
1 and 7 are updated as 4 and 8.
The following subgraph shows vertices and their
distance values, only the vertices with finite distance
values are shown. The vertices included in SPT are
shown in green colour.
Illustration of How to find Shortest
Paths in a graph using Dijkstra’s
Algorithm cont..
Step 2:
Pick the vertex with minimum distance value and not
already included in SPT (not in sptSET). The vertex 1
is picked and added to sptSet.
So sptSet now becomes {0, 1}. Update the distance
values of adjacent vertices of 1.
The distance value of vertex 2 becomes 12.
Illustration of How to find Shortest
Paths in a graph using Dijkstra’s
Algorithm cont..
Step 3:
Pick the vertex with minimum distance value and not
already included in SPT (not in sptSET). Vertex 7 is
picked. So sptSet now becomes {0, 1, 7}.
Update the distance values of adjacent vertices of 7.
The distance value of vertex 6 and 8 becomes finite
(15 and 9 respectively).
Illustration of How to find Shortest
Paths in a graph using Dijkstra’s
Algorithm cont..
Step 4:
Pick the vertex with minimum distance value and not
already included in SPT (not in sptSET). Vertex 6 is
picked. So sptSet now becomes {0, 1, 7, 6}.
Update the distance values of adjacent vertices of 6.
The distance value of vertex 5 and 8 are updated.
Illustration of How to find Shortest
Paths in a graph using Dijkstra’s
Algorithm cont..
Step 4:
Pick the vertex with minimum distance value and not
already included in SPT (not in sptSET). Vertex 6 is
picked. So sptSet now becomes {0, 1, 7, 6}.
Update the distance values of adjacent vertices of 6.
The distance value of vertex 5 and 8 are updated.
Illustration of How to find Shortest
Paths in a graph using Dijkstra’s
Algorithm cont..