Chapter 4 discusses graph algorithms in C/C++, focusing on traversal methods such as Depth-First Search (DFS) and Breadth-First Search (BFS), along with their respective processes and complexities. It also covers shortest path algorithms, particularly Dijkstra's Algorithm, which calculates the minimum cost from a source to a destination node in a graph. The chapter provides detailed steps for implementing these algorithms and their applications in graph data structures.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views27 pages
Chapter 4 - Algorithms in C - Graph Algorithms
Chapter 4 discusses graph algorithms in C/C++, focusing on traversal methods such as Depth-First Search (DFS) and Breadth-First Search (BFS), along with their respective processes and complexities. It also covers shortest path algorithms, particularly Dijkstra's Algorithm, which calculates the minimum cost from a source to a destination node in a graph. The chapter provides detailed steps for implementing these algorithms and their applications in graph data structures.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27
CHAPTER 4: ALGORITHMS IN
C/C++ D ATA S T R U C T U R E a n d A L G O R I T H M S
M.E. LE THANH TUNG
4.5 GRAPH ALGORITHMS:
⚬ Graph traversal problem:
⚬ Graph traversal involves searching a graph data structure that explores all the vertices in a graph ⚬ Depth-first search (DFS): is similar to Depth First Traversal of a tree. ⚬ Breadth-first search (BFS): explores all the vertices in a graph at the current depth before moving on to the vertices at the next depth level. ⚬ Unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we divide the vertices into two categories: Visited and Not 4.5 GRAPH ALGORITHMS:
• 4.5.1 Depth-First Search (DFS):
• How DFS works: ⚬ The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) ⚬ Explores as far as possible along each branch before backtracking. ⚬ Visited array is used to mark the visited vertices. ⚬ 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 + E), since an extra visited array of size V is required, And stack size for iterative call to DFS function. 4.5 GRAPH ALGORITHMS:
• 4.5.1 Depth-First Search (DFS):
• Step 1: • Create the graph with the following data. • Initially stack and visited arrays are empty. 4.5 GRAPH ALGORITHMS:
• 4.5.1 Depth-First Search (DFS):
• Step 2: • Visit 0 (root node) and put it to visited array. • Put its adjacent nodes which are not visited yet into the stack. 4.5 GRAPH ALGORITHMS:
• 4.5.1 Depth-First Search (DFS):
• Step 3: • Node 1 at the top of the stack, so visit node 1: add node to visited array and pop it from the stack. • Put all of its adjacent nodes which are not visited in the stack. 4.5 GRAPH ALGORITHMS:
• 4.5.1 Depth-First Search (DFS):
• Step 4: • Node 2 at the top of the stack, so visit node 2: adds node to visited array and pop it from the stack. • Put all of its adjacent nodes which are not visited in the stack. 4.5 GRAPH ALGORITHMS:
• 4.5.1 Depth-First Search (DFS):
• Step 5: • Node 4 at the top of the stack, so visit node 4: adds node to visited array and pop it from the stack. • Put all of its adjacent nodes which are not visited in the stack. 4.5 GRAPH ALGORITHMS:
• 4.5.1 Depth-First Search (DFS):
• Step 6: • Node 3 at the top of the stack, so visit node 3 and put all of its adjacent nodes which are not visited in the stack. • Stack becomes empty, which means we have visited all the nodes and our DFS traversal ends. 4.5 GRAPH ALGORITHMS:
• 4.5.2 Breath-First Search (BFS):
• How Breath-First traversal works: ⚬ In BFS, 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 pushed into the queue and the nodes of the current level are marked visited and popped from the queue. 4.5 GRAPH ALGORITHMS:
• 4.5.2 Breath-First Search (BFS):
• Step 1: • Create the graph with the following data. • Initially queue and visited arrays are empty. 4.5 GRAPH ALGORITHMS:
• 4.5.2 Breath-First Search (BFS):
• Step 2: • Push node 0 into queue and mark it visited. 4.5 GRAPH ALGORITHMS:
• 4.5.2 Breath-First Search (BFS):
• Step 3: • Remove node 0 from the front of queue and visit the unvisited neighbours and push them into queue. 4.5 GRAPH ALGORITHMS:
• 4.5.2 Breath-First Search (BFS):
• Step 4: • Remove node 1 from the front of queue and visit the unvisited neighbours and push them into queue. 4.5 GRAPH ALGORITHMS:
• 4.5.2 Breath-First Search (BFS):
• Step 5: • Remove node 2 from the front of queue and visit the unvisited neighbours and push them into queue. 4.5 GRAPH ALGORITHMS:
• 4.5.2 Breath-First Search (BFS):
• Step 6: • Remove node 3 from the front of queue and visit the unvisited neighbours and push them into queue. • As we can see that every neighbours of node 3 is visited, so move to the next node that are in the front of the queue. 4.5 GRAPH ALGORITHMS:
• 4.5.2 Breath-First Search (BFS):
• Step 6: • Remove node 4 from the front of queue and visit the unvisited neighbours and push them into queue. • As we can see that every neighbours of node 4 is visited, so move to the next node that are in the front of the queue. 4.5 GRAPH ALGORITHMS:
• 4.5.3 Shortest path algorithms:
⚬ The shortest path algorithms are the ones that focuses on calculating the minimum travelling cost from source node to destination node of a graph in optimal time and space complexities. 4.5 GRAPH ALGORITHMS:
• 4.5.3 Shortest path algorithms:
• Dijkstra’s Algorithm: • Dijkstra’s algorithm is a popular algorithms for solving many single- source shortest path problems having non-negative edge weight in the graphs. It was conceived by Dutch computer scientist Edsger W. Dijkstra in 1956: ⚬ It starts at the source vertex and iteratively selects the unvisited vertex with the smallest tentative distance from the source. ⚬ It then visits the neighbors of this vertex and updates their tentative distances if a shorter path is found. ⚬ This process continues until the destination vertex is reached, or 4.5 GRAPH ALGORITHMS:
• 4.5.3 Shortest path algorithms:
• Dijkstra’s Algorithm: • Dijkstra’s algorithm is a popular algorithms for solving many single- source shortest path problems having non-negative edge weight in the graphs. It was conceived by Dutch computer scientist Edsger W. Dijkstra in 1956: ⚬ It starts at the source vertex and iteratively selects the unvisited vertex with the smallest tentative distance from the source. ⚬ It then visits the neighbors of this vertex and updates their tentative distances if a shorter path is found. ⚬ This process continues until the destination vertex is reached, or 4.5 GRAPH ALGORITHMS: