0% found this document useful (0 votes)
25 views

Httpslms Ou Edu Vn241pluginfile Php217729mod - resourcecontent1DsA-04-Graph PDF

Uploaded by

2351010114long
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Httpslms Ou Edu Vn241pluginfile Php217729mod - resourcecontent1DsA-04-Graph PDF

Uploaded by

2351010114long
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

DATA STRUCTURES & ALGORITHMS

Graphs and Applications

Ngo Quoc Viet-2023


Contents
• Introduction to graph.
• Graph implementation.
• Adjacent matrix
• Linked list
• Breath First Search.
• Depth First Search.
Learning outcome
• Understanding graphs and applictions.
• Using adjacent matrix and linked list to represent a graph
• Being able to implement DFS and BFS algorithms.
Introduction to graph
• A graph is a discrete structure consisting of vertices and edges
connecting vertices
• Graph 𝐺 = (𝑉, 𝐸), in which V is set of vertices (also called nodes), E
is set of edges.
• Edge is a pair (v, w) where v, w are in V. The edges may contain V
a b
weight/value/cost.
• Path: A path from vertex V to Z is a sequence of edges that can d
U X Z
be followed starting from V to reach Z. h
c e
• Can be represented as vertices visited, or edges taken
• Example, one path from V to Z: {b, h} or {V, X, Z} W g
• What are two paths from U to Y?
f
• Path length: Number of vertices or edges contained in the path. Y
• Neighbor or adjacent: Two vertices connected directly by an
edge, example V and X.
Introduction to graph
• Cycle: A path that begins and ends at the same V
a b
node
d
• Example: {b, g, f, c, a} or {V, X, Y, W, U, V}. U X
h
Z
• Example: {c, d, a} or {U, W, V, U}. c e
W g
• Acyclic graph: One that does not contain any
f
cycles. Y

• Loop: An edge directly from a node to itself. a b

• Directed graph ("digraph"): One where edges c d e


are one-way connections between vertices
f g
Graph implementation
• Define an n × n matrix 𝐴 = (𝑎𝑖𝑗 ) and if the vertices vi and vj are connected
with weight w, then set 𝑎𝑖𝑗 = 𝑤 and 𝑎𝑗𝑖 = 𝑤
• That is, the matrix is symmetric, e.g.,

• If the graph is unweighted, the adjacency matrix may contain boolean values.
• We can use 2D array to represent a graph.
Graph implementation
• If the graph was directed, then the matrix would not necessarily be
symmetric
Sparse matrix
• Matrices where less than 5% of the entries are not the default value
(either infinity or 0, or perhaps some other default value) are said
to be sparse.
• Matrices where most entries (25% or more) are not the default
value are said to be dense

• A sparse matrix is suitable for graphs with few edges


• A adjacent list can be used to represent sparse matrices
Adjacency list for graph
• An array of lists is used. The size of the
array is equal to the number of vertices.
An entry array[i] represents the list of vertices
adjacent to the ith vertex.
• This representation can also be used to
represent a weighted graph. The weights of
edges can be represented as lists of pairs.
• We can use <vector> in STL to represent a
list of vertices.

Write your function to create this graph with


adjacent list

Class Assignment: please declare complete structures for adjacent list to store a graph.
Adjacency list for graph

https://www.geeksforgeeks.org/graph-and-its-
representations/
Traversal algorithms in graph
• Graph Traversal on the graph: Traversal means visiting all the nodes of
a graph. All vertices in a graph need to be visited exactly once
• Graph Traversal is also used to decide the order of vertices to be visited
in the search process.
• A graph traversal finds the edges to be used in the search process
without creating loops. This means that, with graph traversal, we can visit
all the vertices of the graph without getting into a looping path. There
are two graph traversal techniques
• DFS (Depth First Search)
• BFS (Breadth-First Search)
• BFS and DFS are bases for many graph related algorithms
BFS applications
1. Shortest Path and Minimum Spanning Tree for unweighted graph
2. Peer to Peer Networks: to find all neighbor nodes
3. Crawlers in Search Engines
4. Social Networking Websites: to find people within a given distance k from a person.
5. GPS Navigation systems: to find all neighboring locations
6. Cycle detection in undirected graph
7. Ford–Fulkerson algorithm: to find the maximum flow.
8. Path Finding: to find if there is a path between two vertices
9. To test if a graph is Bipartite.
Many algorithms like Prim’s Minimum Spanning Tree and Dijkstra’s Single Source Shortest Path
use structure similar to Breadth First Search
BFS Algorithm
• Breadth-first search (BFS) is an algorithm that is used to graph data
or searching tree or traversing structures.
• This algorithm selects a single node (initial or source point) in a
graph and then visits all the nodes adjacent to the selected node
• Once the algorithm visits and marks the starting node, then it
moves towards the nearest unvisited nodes and analyses them.
Once visited, all nodes are marked. These iterations continue until
all the nodes of the graph have been successfully visited and
marked
Why BFS Algorithm
• BFS is useful for analyzing the nodes in a graph and constructing
the shortest path of traversing through these
• BFS can traverse through a graph in the smallest number of
iterations
• The architecture of the BFS algorithm is simple and robust
• The result of the BFS algorithm holds a high level of accuracy in
comparison to other algorithms
• BFS iterations are seamless, and there is no possibility of this
algorithm getting caught up in an infinite loop problem
How BFS works
Start from vertex 0, putting it in the Visited list and
putting all its adjacent vertices in the stack

• Breadth First Search (BFS) algorithm


traverses a graph in a breadthward
motion and uses a queue to remember to Visit the element at the front of queue i.e. 1 and
go to its adjacent nodes. Since 0 has already been
get the next vertex to start a search, when visited, we visit 2 instead
a dead end occurs in any iteration.
• It employs the following rules
• Rule 1 − Visit the adjacent unvisited vertex.
Mark it as visited. Display it. Insert it in a queue Vertex 2 has an unvisited adjacent vertex in 4, so
• Rule 2 − If no adjacent vertex is found, we add that to the back of the queue and visit 3,
remove the first vertex from the queue which is at the front of the queue

• Rule 3 − Repeat Rule 1 and Rule 2 until the


queue is empty
Implementation of BFS traversal
• Declare a queue and insert the starting Visit 2 which was added to queue earlier to add its
vertex neighbours

• Initialize a visited array and mark the


starting vertex as visited
• Follow the below process till the queue
becomes empty
• Remove the first vertex of the queue Only 4 remains in the queue since the only
adjacent node of 3 i.e. 0 is already visited. We visit
• Mark that vertex as visited it
• Insert all the unvisited neighbours of the vertex
into the queue
• Time Complexity: O(V+E), where V is the
number of nodes and E is the number of
edges.
BFS example
Push the first vertex onto the Pop A and push B, C and E
queue A

A B C E
BFS example
Pop B and push D Pop C, push F
A B A B C

C E D E D F
BFS example
Pop E and push G and H Pop D
A B C E A B C E D

D F G H F G H
BFS example
Pop F Pop G and push I
A B C E D F A B C E D F G

G H H I
BFS example
Pop H Pop I
A B C E D F G H A B C E D F G H I

I
Rules of BFS Algorithm
• A queue (FIFO-First in First Out) data structure is used by BFS.
• You mark any node in the graph as root and start traversing the data from it.
• BFS traverses all the nodes in the graph and keeps dropping them as
completed.
• BFS visits an adjacent unvisited node, marks it as done, and inserts it into a
queue.
• Removes the previous vertex from the queue in case no adjacent vertex is
found.
• BFS algorithm iterates until all the vertices in the graph are successfully
traversed and marked as completed.
• There are no loops caused by BFS during the traversing of data from any node
BFS Implemtation
1. Create a queue and #include <queue>
enqueue/insert source into it vector<bool> v; vector<vector<int> > g;
i. Mark source as visited queue<int> q; q.push(u); v[u] = true;
2. While queue is not empty, do while (!q.empty())
following int f = q.front(); q.pop();
i. Dequeue/Remove a vertex from v[f] = true;
queue. Let this be f. for (auto i = g[f].begin(); i != g[f].end(); i++)
ii. Mark that vertex as visited if (!v[*i]) {
iii. Enqueue/Insert all the unvisited q.push(*i);
neighbours of the vertex into the v[*i] = true;
queue.
}
DFS applications
• Detecting cycle in a graph
• Path Finding
• Topological Sorting: to scheduling jobs from given dependencies
among jobs. Topological sorting can be done using DFS algorithm
• To test if a graph is bipartite
• Finding Strongly Connected Components of a graph: If there is a
path from each vertex to every other vertex, that is strongly
connected.
Starts by putting it in the Visited list and putting

DFS Algorithm all its adjacent vertices in the stack

• Depth first Search or Depth first traversal is a recursive


algorithm for searching all the vertices of a graph or
tree data structure. Traversal means visiting all the
nodes of a graph Next, visit the element at the top of stack i.e. 1
and go to its adjacent nodes. Since 0 has already
• Depth First Search (DFS) algorithm traverses a graph in been visited, we visit 2 instead
a depthward motion and uses a stack to remember to
get the next vertex to start a search, when a dead end
occurs in any iteration
• It employs the following rules
• Rule 1: Visit the adjacent unvisited vertex. Mark it as visited. Vertex 2 has an unvisited adjacent vertex in 4, so
Display it. Push it in a stack. we add that to the top of the stack and visit it
• Rule 2: If no adjacent vertex is found, pop up a vertex from
the stack. (It will pop up all the vertices from the stack, which
do not have adjacent vertices.)
• Rule 3: Repeat Rule 1 and Rule 2 until the stack is empty
Implementation of DFS traversal
1. Declare a stack and insert any one of the graph's Vertex 2 has an unvisited adjacent vertex in 4, so
vertices on top of a stack we add that to the top of the stack and visit it

2. Take the top item of the stack and add it to the


visited list
3. Create a list of that vertex's adjacent nodes. Add the
ones which aren't in the visited list to the top of the
stack After we visit the last element 3, it doesn't have
4. Keep repeating steps 2 and 3 until the stack is empty any unvisited adjacent nodes, so we have
completed the Depth First Traversal of the graph

• Time complexity: O(V + E), where V is the number of


vertices and E is the number of edges in the graph.
• Space Complexity: O(V), since an extra visited array of
size V is required
Implementation of DFS traversal
function dfs(v1, v2):
• Create a recursive function that takes dfs(v1, v2, { }).
the index of the node and a visited
array function dfs(v1, v2, path):
path += v1.
• Mark the current node as visited mark v1 as visited.
if v1 is v2:
• Traverse all the adjacent and
a path is found!
unmarked nodes and call the for each unvisited neighbor v of v1:
recursive function with the index of
the adjacent node if dfs(v, v2, path)
finds a path: a path is found!

path -= v1. // path is not found.


DFS Iterative example

A A B A B C

B C E C E D E D F
DFS Iterative example

A B C E A B C E D A B C E D F

D F G H F G H G H
DFS Iterative example

A B C E D F G A B C E D F G H A B C E D F G H I

H I I
DFS Recursive example

A A B A B C
DFS Recursive example

A B C D A B C D E A B C D E G
DFS Recursive example

A B C D E G I A B C D E G I H A B C D E G I H F
DFS-Iterative & Recursive
• An iterative DFS may also be different from recursive DFS

An iterative DFS An recursive DFS

A B C E D F G H I A B C D E G I H F
DFS Iteraative Implemtation
1. Create a stack and insert source into it. stack<int> stack; stack.push(v);
Declared visited vector vector <bool> visited(n, false);

2. While stack is not empty, do while (!stack.empty())

i. Pop a vertex from stack, called s. s = stack.top(); stack.pop();


ii. Check if vertex is not visited, keep it, if(!visited[s])
and mard it as visited cout << s << " ";
visited[s] = true;
iii. do for every edge (s, t)
for(auto i = adj[v].begin(); i != adj[v].end(); ++i)
if(!visited[*i])
stack.push(*i);

https://www.techiedelight.com/depth-first-search/
https://www.tutorialcup.com/interview/graph/iterative-depth-first-traversal-of-graph.htm
Summary
• We can use adjacent matrix or linked list to represent a graph.
• Two most popular graph traversals
• Breath First Search.
• Depth First Search
• BFS uses queue and DFS uses stack to implement
• We can use recursive or iterative (associate with stack) approach to
implement DFS

You might also like