0% found this document useful (0 votes)
115 views24 pages

18-Dial''s Algorithm-23-02-2024

The document discusses Dial's Algorithm, which is an enhancement of Dijkstra's Algorithm for finding the shortest path between two nodes in a weighted graph. It works by organizing nodes into buckets based on distance from the source node and iteratively exploring closer nodes. Pseudocode and a walkthrough example are provided. Time and space complexity analysis is also included.

Uploaded by

kshitij
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
115 views24 pages

18-Dial''s Algorithm-23-02-2024

The document discusses Dial's Algorithm, which is an enhancement of Dijkstra's Algorithm for finding the shortest path between two nodes in a weighted graph. It works by organizing nodes into buckets based on distance from the source node and iteratively exploring closer nodes. Pseudocode and a walkthrough example are provided. Time and space complexity analysis is also included.

Uploaded by

kshitij
Copyright
© © All Rights Reserved
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/ 24

DIAL’S ALGORITHM

TEST TIME ON THE CELEBRITY PROBLEM

URL:
DIAL’S ALGORITHM

EXPLANATION

Dial's Algorithm is a graph traversal algorithm used for finding the shortest

path between two nodes in a weighted graph.

It is an enhancement of Dijkstra's Algorithm and is particularly efficient when

dealing with graphs where edge weights have a limited range.

Dial's Algorithm works by organizing nodes into buckets based on their distance

from the source node and iteratively exploring nodes in increasing order of

distance.
DIAL’S ALGORITHM

ALGORITHM

Initialize a set of buckets, one for each possible distance from the source

node.

Place the source node in the bucket corresponding to its distance (usually

bucket 0).

Initialize a variable to keep track of the current distance (starting from 0).
DIAL’S ALGORITHM

ALGORITHM

While there are non-empty buckets:

✔ Select the first non-empty bucket with the lowest distance.

✔ Remove the node with the shortest distance from the selected bucket.

✔ Update the distances of its neighbors if a shorter path is found.

✔ Move the node to a new bucket corresponding to its updated distance.

✔ Increment the current distance.

Continue until the target node is reached or all buckets are empty.
DIAL’S ALGORITHM
PSEUDOCODE
function Dial'sAlgorithm(graph, source,
Update distances of its neighbors if
target):
shorter path found
Initialize buckets
Move the node to a new bucket based
Place source node in the corresponding on updated distance
bucket
Increment current distance
Initialize current distance to 0
if target node is reached:
while there are non-empty buckets:
return shortest path to target
Select the first non-empty bucket
return "No path exists"
with the lowest distance

Remove the node with the shortest


distance from the bucket
DIAL’S ALGORITHM
EXAMPLE

ASSIGN START POINT AT NODE “0”

1 3 2
2 START
0 2 1
1
3 4 4
DIAL’S ALGORITHM
EXAMPLE

Consider node 0

Current node Neighbour node weight


1 3 2
0 0 --
2
0 2 1
1
3 4 4
DIAL’S ALGORITHM
EXAMPLE

Consider node 1

Start node current node weight


1 3 2
0 1 2
2
0 2 1
1
3 4 4
DIAL’S ALGORITHM
EXAMPLE

Consider node 2

Start node current node weight


1 3 2
0 2 5(BEST PATH)
2
2 0 2 6(1+4+1)
0 1
1
3 4 4
DIAL’S ALGORITHM
EXAMPLE

Consider node 3

Start node current node weight


1 3 2
0 3 1(BEST PATH)
2
2 0 3 4(2+2)
0 1
1
3 4 4
DIAL’S ALGORITHM
EXAMPLE

Consider node 4

Start node current node weight


1 3 2
0 4 5(1+4) (BEST PATH)
2
2 0 4 8(2+2+4)
0 1
1
3 4 4
DIAL’S ALGORITHM
EXAMPLE

Start node End node weight

0 0
1 3 2
1 2
2
0 2 5
0 2 1
3 1
1 4 5
3 4 4
DIAL’S ALGORITHM

import java.util.*; int[] distance = new int[V];


class Graph { Arrays.fill(distance,
private int V; Integer.MAX_VALUE);
private List<List<Node>> adj;
distance[startVertex] = 0;
public Graph(int V) {
this.V = V; PriorityQueue<Node> pq = new
adj = new ArrayList<>(V); PriorityQueue<>(V,
for (int i = 0; i < V; i++) { Comparator.comparingInt(node ->
adj.add(new ArrayList<>()); node.weight));
} pq.add(new Node(startVertex, 0));
} while (!pq.isEmpty()) {
public void addEdge(int source, int int currentVertex =
destination, int weight) { pq.poll().vertex;
Node node = new Node(destination, for (Node neighbor :
weight); adj.get(currentVertex)) {
adj.get(source).add(node); int newDist =
} distance[currentVertex] +
public void dijkstra(int startVertex) neighbor.weight;
{
DIAL’S ALGORITHM

if (newDist < distance[neighbor.vertex]) this.vertex = vertex;


{ this.weight = weight;
distance[neighbor.vertex] = newDist; }
pq.add(new Node(neighbor.vertex, }
newDist)); }
} public class Main {
} public static void main(String[]
} args) {
// Print the distances int V = 5; // Number of vertices
System.out.println("Vertex\tDistance from int source = 0; // Source vertex
Source"); Graph graph = new Graph(V);
for (int i = 0; i < V; i++) { graph.addEdge(0, 1, 2);
System.out.println(i + "\t" + graph.addEdge(0, 3, 1);
distance[i]); graph.addEdge(1, 2, 3);
} graph.addEdge(1, 3, 2);
} graph.addEdge(3, 4, 4);
static class Node { graph.addEdge(4, 2, 1);
int vertex;
int weight; graph.dijkstra(source);
public Node(int vertex, int weight) }
{ }
DIAL’S ALGORITHM

TIME AND SPACE COMPLEXITY

Time Complexity

The time complexity of this Dijkstra's algorithm implementation is O(V^2) for the

worst-case scenario when using an adjacency matrix.

With the use of a priority queue, the time complexity can be reduced to O(E + V *

log(V)), where E is the number of edges and V is the number of vertices.


DIAL’S ALGORITHM

TIME AND SPACE COMPLEXITY


Space Complexity

The space complexity of this code is O(V^2) for the adjacency matrix representation

due to the 2D array.

With an adjacency list representation, the space complexity is O(V + E), where V is

the number of vertices and E is the number of edges in the graph.


INTERVIEW QUESTIONS

1. What is Dijkstra's algorithm, and what problem does it solve?

• Dijkstra's algorithm is a graph traversal algorithm that finds the

shortest path from a source node to all other nodes in a weighted graph.

• It solves the single-source shortest path problem, which is finding the

shortest path from one node to all other nodes while considering edge

weights.
INTERVIEW QUESTIONS

2. How does Dijkstra's algorithm work?


• Dijkstra's algorithm starts with a source node and explores neighboring

nodes, updating the shortest distances.

• It maintains a priority queue or set to select the node with the

smallest tentative distance at each step.

• The algorithm iterates until all nodes are visited or the target node is

reached.
INTERVIEW QUESTIONS

3. What is the difference between Dijkstra's algorithm and BFS


(Breadth-First Search)?

Dijkstra's algorithm finds the shortest path in weighted graphs,

considering edge weights.

BFS explores nodes in unweighted graphs without considering edge weights

to find the shortest path in terms of the number of edges.


INTERVIEW QUESTIONS

4. Can Dijkstra's algorithm handle graphs with negative edge weights?

No, Dijkstra's algorithm does not work correctly with negative edge

weights.

Negative edge weights can lead to incorrect results and are better

suited for algorithms like Bellman-Ford.


INTERVIEW QUESTIONS

5. When should you choose Dijkstra's algorithm over other graph


algorithms?

Use Dijkstra's algorithm when you need to find the shortest path in a

weighted graph with non-negative edge weights.

It is particularly useful for problems like finding the shortest route

on a map or optimizing network routing.


/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

[email protected] +91 7815 095 095 +91 9019 921 340

You might also like