GROUP 15 Elementary graph algorithm
GROUP 15 Elementary graph algorithm
PRESENTATION
1. SALAUDEEN ABDULLAHI AKINKUNMI- 21/52HA128
2. SAMUEL DEBORAH MOYINOLUWA – 22/25PJ123
3. OYETAYO EMMANUEL TEMITOPE – 22/25PJ122
4. ORIOLA MUYIDEEN OLAWALE- 22/25PJ121
5. YAQUB OLAWALE AYATULAI – 22/52HA203
6. OYELEKE CALEB ABIODUN – 22/52HA200
7. MCFADIPE EZEKIEL OLUWATOBI – 22/52HA195
8. OMOROTIONMWAN FAVOUR ISOKEN – 22/52HA199
9. OLAOGUN RASHEED AKOREDE – 22/52HA197
10. KOLADE AYODEJI ISRAEL – 22/52HA194
11. SANNI USWAT OREOLUWA- 22/52HA202
12. SAMSON AYOMIDE SAMSON- 22/52HA201
13. MOSHOOD ABDULMATEEN- 22/52HA196
TOPICS
Topological Sort.
DSA GROUP 15
Elementary Graph
Algorithms and
Representation of
Graph.
DSA GROUP 15
WHAT IS A GRAPH?
A Graph is a non-linear
data structure that consists
of vertices (nodes) and
edges(connection between
nodes).
,
DSA GROUP 15
Directed
and
Undirected
Weighted
Cyclic and
and
Unweighted Acyclic
Sparse Dense
Graph Graph
TYPES OF
GRAPHS
DSA GROUP 15
DIRECTED GRAPH UNDIRECTED GRAPH
A weighted graph is a type of graph This is a graph where all edges are
where each edge has a numerical value
called a weight associated with it. considered equal, with no numerical
values or weight assigned to them.
Representaion: Edges are represented as
pairs along with their weight e.g (A,B,5)
where 5 is the weight of the edge between A Representation: Edges are represented
and B.
simply as pairs of vertices (A,B)
Characteristics:
-Weight can represent distance or travel
times between cities. Characteristics
-Computer networks: Representing bandwith -Social networks where edges indicate
or latency between nodes.
-Resource allocation: Representing costs or friendship but don’t show their strength.
capacities.
DSA GROUP 15
DSA GROUP 15
CYCLIC GRAPH ACYCLIC GRAPH
A cyclic graph is a graph that contains at An acyclic graph is a graph that does not
least one cycle. contain any cycles.
Representation:
Adjacency matrix: A square matrix where Representation: Similar to cyclic graphs
each element a[i][j] represents the presence (Adjacency Matrix and List), but traversal will
(1) or absence (0) of an edge between not encounter repeated visits forming a
vertices i and j. Cycles can be observed cycle.
through matrix traversal.
Characteristics:
Adjacency list: Each vertex has a list of -They have a topological ordering that is
vertices it is connected to. Cycles are nodes can be arranged linearly such that
identified by detecting repeated visits to every directed edge point from an earlier
vertices during traversal. node to a later node.
Characteristics: -They are common in representing
- Contains at least one closed loop workflows, tasks and data dependencies.
- Found in both directed and undirected
graphs.
DSA GROUP 15
DSA GROUP 15
SPARSE GRAPH DENSE GRAPH
There are few edges compared to the There are many edges, close to the
number of vertices. maximum possible.
DSA GROUP 15
A Graph representation tells us how a Graph is stored in memory.
- Be better suited depending on what type of Graph we have (weighted, directed, etc.), and
DSA GROUP 15
Adjacency Matrix Adjacency List Graph
An adjacency matrix is a way to In case we have a 'sparse' Graph with
represent a graph using a grid or table. many vertices, we can save space by
This grid helps us quickly figure out using an Adjacency List because an
which nodes (points) in the graph are Adjacency Matrix would reserve a lot of
connected to each other. memory on empty Array elements for
The Adjacency Matrix is a 2D array edges that don't exist.
(matrix) where each cell on index (i, j) An Adjacency List has an array that
stores information about the edge from contains all the vertices in the Graph,
vertex i to vertex j. and each vertex has a Linked List (or
Array) with the vertex's edges.
DSA GROUP 15
EDGE LIST
An edge list is a way to represent a graph by listing all the connections (called
edges) between the nodes. Each edge is written as a pair or a triplet, depending on
the type of graph. Think of it as a list of friendships: every connection between two
people is written down.
For example: If A is connected to B, you write (A, B). There's no need to write (B,
A) separately.
Example:
Let’s say we have 3 nodes: A, B, and C.
Connections: A B, A C
Edge List: (A, B),(A, C)
DSA GROUP 15
BREADTH FIRST
SEARCH/DEPTH
FIRST SEARCH
DSA GROUP 15
Breadth-First Search (BFS) is one of the simplest
and most widely used algorithms for
WHAT IS searching a graph. It systematically explores all
BREADTH nodes in the graph to find a solution, starting
FIRST SEARCH? from a given starting point (or root node) and
moving outward layer by layer.
Key Terminologies in BFS include: Graph,
Node(vertex), Edge, Queue, Visited Set, Root and
Level.
DSA GROUP 15
BREADTH FIRST SEARCH
1. Purpose: BFS is used to traverse or search a graph, layer by layer, exploring all
neighbors of a node before moving deeper.
2. Approach: BFS uses a queue (FIFO structure) to keep track of nodes.
3. How It Works:
- Start from a source node
- Visit all its direct neighbors (level 1)
- Move to the next level of neighbors (level 2)
- Repeat all nodes are visited
4. It is Best For: finding the shortest path in an unweighted graph.
5. Time complexity: O(V + E), where V is the number of vertices and E is the number of
edges.
DSA GROUP 15
Depth First Search (DFS) is a popular algorithm
used for traversing or searching through
graphs and trees. It explores as far as possible
OKAY, SO HOW
along a branch before backtracking, making
ABOUT
it useful for solving problems that involve
DEPTH FIRST
searching all possible paths, such as mazes,
SEARCH?
puzzles, or network analysis.
DSA GROUP 15
DEPTH FIRST SEARCH
1. Purpose : DFS is used to explore as far as possible along one branch before
backtracking.
2. Approach: DFS uses a stack ( can be implemented using recursion or an explicit
stack)
3. How It Works :
- Start from a source node.
- Visit a neighbor and go as deep as possible.
- Backtrack when no further neighbors are available and explore the next branch.
- Repeat until all nodes are visited.
4. It is Best For: detective cycles, topological sorting, and connecting checks.
5. Time complexity:O(V+E), same as BFS.
DSA GROUP 15
EXAMPLE OF BFS EXAMPLE OF DFS
DSA GROUP 15
APPLICATIONS
BFS DFS
DSA GROUP 15
Topological sorting for Directed Acyclic Graph
(DAG) is a linear ordering of vertices such that for
every directed edge u--->v, vertex u comes before v in
the ordering. It is also a dependency problem in which
completion of one task depends upon the completion
of several other tasks whose order can vary.
DSA GROUP 15
Note: Topological Sorting for a graph is not possible if the graph is not a DAG.
The most common and efficient way a topological sort can be done is with
1. Depth-First Search approach
2. Kahn’s Algorithm (Breadth-First Search approach)
DSA GROUP 15
Here’s a step-by-step algorithm for topological sorting using Depth First Search (DFS):
1. Create a graph with n vertices and m-directed edges.
2. Initialize a stack and a visited array of size n.
3. For each unvisited vertex in the graph, do the following:
• Call the DFS function with the vertex as the parameter.
• In the DFS function, mark the vertex as visited and recursively call the DFS function for all unvisited
neighbors of the vertex.
• Once all the neighbors have been visited, push the vertex onto the stack.
4. After all, vertices have been visited, pop elements from the stack and append them to the output list
until the stack is empty.
5. The resulting list is the topologically sorted order of the graph.
DSA GROUP 15
EXAMPLE OF TOPOLOGICAL
SORTING
Input: Graph
THE TIME
COMPLEXITY FOR
TOPOLOGICAL
SORT IS O(V+E)
DSA GROUP 15
ADVANTAGES DISADVANTAGES
1. Helps in scheduling tasks or 1. Only applicable to directed
events based on dependencies. acyclic graphs (DAGs), not
2. Detects cycles in a directed suitable for cyclic graphs.
graph. 2. May not be unique, multiple
3. Efficient for solving problems valid topological orderings can
with precedence constraints. exist.
APPLICATIONS
DSA GROUP 15
MINIMUM
SPANNING TREE
DSA GROUP 15
A spanning tree is defined as a tree-like subgraph of a connected, undirected graph that
includes all the vertices of the graph. Or, to say in Layman’s words, it is a subset of the
edges of the graph that forms a tree (acyclic) where every node of the graph is a part of
the tree. There is a fixed number of edges in the spanning tree which is equal to one less
than the total number of vertices ( E = V-1 ).
WHAT IS A
SPANNING
TREE?
DSA GROUP 15
The minimum spanning tree has all the
properties of a spanning tree with an added A minimum spanning
constraint of having the minimum possible tree (MST) is defined as
weights among all possible spanning trees. a spanning tree that has
Like a spanning tree, there can also be many the minimum weight
among all the possible
possible MSTs for a graph.
spanning trees.
DSA GROUP 15
There are many algorithms that can be used to
DSA GROUP 15
PRIM’S ALGORITHM
Prim’s algorithm, like the other two aforementioned, is a Greedy algorithm. This algorithm always
starts with a single node and moves through several adjacent nodes, in order to explore all of the
The algorithm starts with an empty spanning tree. The idea is to maintain two sets of vertices. The
first set contains the vertices already included in the MST, and the other set contains the vertices not
yet included. At every step, it considers all the edges that connect the two sets and picks the
minimum weight edge from these edges. After picking the edge, it moves the other endpoint of the
DSA GROUP 15
DSA GROUP 15
The final structure of the MST is as follows
and the weight of the edges of the MST is (4
+ 8 + 1 + 2 + 4 + 2 + 7 + 9) = 37.
It has a time complexity of O((V+E)logV).
DSA GROUP 15
DSA GROUP 15
THANK YOU
DSA GROUP 15