The document discusses network protocols and algorithms, focusing on Dijkstra's and Bellman-Ford algorithms for finding the shortest paths in graphs. It explains basic graph concepts, types of graphs, and the requirements and workings of both algorithms, highlighting their applications and differences, particularly in handling edge weights. Additionally, it includes review questions to test understanding of the algorithms' principles and functionalities.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
9 views29 pages
2-2 Routing Algorithms
The document discusses network protocols and algorithms, focusing on Dijkstra's and Bellman-Ford algorithms for finding the shortest paths in graphs. It explains basic graph concepts, types of graphs, and the requirements and workings of both algorithms, highlighting their applications and differences, particularly in handling edge weights. Additionally, it includes review questions to test understanding of the algorithms' principles and functionalities.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29
Network protocols and
Algorithms
Dijkstra and Bellman-Ford
Algorithms Basic Concepts • Graphs are data structures used to represent "connections" between pairs of elements. • These elements are called nodes (vertices). They represent real-life objects, persons, or entities. • The connections between nodes are called edges. • This is a graphical representation of a graph: Applications of Graphs • Graphs are directly applicable to real-world scenarios. For example, we could use graphs to model a transportation network where nodes would represent facilities that send or receive products and edges would represent roads or paths that connect them.
Network represented with a graph
Types of Graphs • Undirected: if for every pair of connected nodes, you can go from one node to the other in both directions. • Directed: if for every pair of connected nodes, you can only go from one node to another in a specific direction. We use arrows instead of simple lines to represent directed edges. Weighted Graphs • Weighted Graphs: is a graph whose edges have a "weight" or "cost". The weight of an edge can represent distance, time, or anything that models the "connection" between the pair of nodes it connects. • These weights are essential for Dijkstra's Algorithm. Dijkstra Algorithm • Dijkstra's Algorithm used to find the shortest path between nodes in a graph. Particularly, it can find the shortest path from a node (called the "source node") to all other nodes in the graph, producing a shortest-path tree. • This algorithm is used in GPS devices to find the shortest path between the current location and the destination. • It has broad applications in industry, specially in domains that require modeling networks. Basics of Dijkstra's Algorithm • Dijkstra's Algorithm basically starts at the source node and it analyzes the graph to find the shortest path between that node and all the other nodes in the graph. • The algorithm keeps track of the currently known shortest distance from each node to the source node and it updates these values if it finds a shorter path. • Once the algorithm has found the shortest path between the source node and another node, that node is marked as "visited" and added to the path. Basics of Dijkstra's Algorithm • The process continues until all the nodes in the graph have been added to the path. This way, it produces the path that connects the source node to all other nodes following the shortest path possible to reach each node. Requirements of Dijkstra Algorithm • Dijkstra's Algorithm can only work with graphs that have positive weights. This is because, during the process, the weights of the edges have to be added to find the shortest path. • If there is a negative weight in the graph, then the algorithm will not work properly. Bellman-Ford Algorithm • Is a single source shortest path algorithm. • It finds the shortest path from a single node to all other nodes in a weighted graph. • It can work on graphs that have negative edge weights. • It can be used on both weighted and unweighted graphs. • It is based on the “Principle of Relaxation”. • It cannot find the shortest path if there exists a negative cycle in the graph. • It is slower than Dijkstra's algorithm , but more versatile. Bellman-Ford Algorithm • How it works: • Set initial distance to zero for the source vertex, and set initial distances to infinity for all other vertices. • For each edge, check if a shorter distance can be calculated, and update the distance if the calculated distance is shorter. • Check all edges (step 2)−1 times. This is as many times as there are vertices, minus one. • Optional: Check for negative cycles. This will be explained in better detail later. Bellman-Ford Algorithm • Find the shortest paths from node A to all other nodes. Bellman-Ford Algorithm • Find the shortest paths from node A to all other nodes. Excercise • Find the shortest paths from node a to all other nodes. Review Questions 1. The Bellmann Ford algorithm returns _____ value. a) Boolean b) Integer c) String d) Double 2. Bellmann ford algorithm provides solution for ____________ problems. a) All pair shortest path b) Sorting c) Network flow d) Single source shortest path 3. How many solution/solutions are available for a graph having negative weight cycle? a) One solution b) Two solutions c) No solution d) Infinite solutions 4. How many times the for loop in the Bellmann Ford Algorithm gets executed? a) V times b) V-1 c) E d) E-1 5. What is the basic principle behind Bellmann Ford Algorithm? a) Interpolation b) Extrapolation c) Regression d) Relaxation 6. Bellmann Ford Algorithm can be applied for ______ a) Undirected and weighted graphs b) Undirected and unweighted graphs c) Directed and weighted graphs d) All directed graphs 5. What is the basic principle behind Bellmann Ford Algorithm? a) Interpolation b) Extrapolation c) Regression d) Relaxation 1. Dijkstra’s Algorithm is used to solve ______ problems. a) All pair shortest path b) Single source shortest path c) Network flow d) Sorting 2. Which of the following is the most commonly used data structure for implementing Dijkstra’s Algorithm? a) Max priority queue b) Stack c) Circular queue d) Min priority queue 3. What is the time complexity of Dijikstra’s algorithm? a) O(N) b) O(N3) c) O(N2) d) O(logN) 4. Dijkstra’s Algorithm cannot be applied on _________. a) Directed and weighted graphs b) Graphs having negative weight function c) Unweighted graphs d) Undirected and unweighted graphs 5. The running time of Bellmann Ford algorithm is lower than that of Dijkstra’s Algorithm. a) True b) False 5. Consider the following graph. If b is the source vertex, what is the minimum cost to reach f vertex? a) 8 b) 9 c) 4 d) 6