How to store the data for multiple objectives in Shortest path search Algorithms ?
Last Updated :
27 Mar, 2023
There are several ways to store the data for multiple objectives in shortest-path search algorithms in C++.
One way is to use a data structure such as a priority queue or a heap that allows you to store the data in a sorted manner, with the elements being ordered according to their cost.
For example, use a std::priority_queue from the C++ Standard Template Library (STL) to store the data for your shortest path search algorithm. The priority queue allows you to insert elements with a certain priority, and it will automatically maintain the queue in a sorted order based on the priority of the elements.
Here is an example of how to use a priority queue to store the data for a shortest-path search algorithm in C++ and Java:
C++
#include <iostream>
#include <queue>
#include <vector>
struct Node {
int id;
int cost;
};
struct Compare {
bool operator()( const Node& lhs, const Node& rhs)
{
return lhs.cost > rhs.cost;
}
};
std::priority_queue<Node, std::vector<Node>, Compare> pq;
int main()
{
pq.push({ 1, 5 });
pq.push({ 2, 2 });
pq.push({ 3, 8 });
pq.push({ 4, 1 });
while (!pq.empty()) {
Node node = pq.top();
pq.pop();
std::cout << "Node ID: " << node.id
<< ", Cost: " << node.cost << std::endl;
}
return 0;
}
|
Java
import java.util.*;
class Node {
int id;
int cost;
Node( int id, int cost)
{
this .id = id;
this .cost = cost;
}
}
class Compare implements Comparator<Node> {
public int compare(Node lhs, Node rhs)
{
return lhs.cost - rhs.cost;
}
}
class Main {
static PriorityQueue<Node> pq
= new PriorityQueue<Node>( new Compare());
public static void main(String[] args)
{
pq.add( new Node( 1 , 5 ));
pq.add( new Node( 2 , 2 ));
pq.add( new Node( 3 , 8 ));
pq.add( new Node( 4 , 1 ));
while (!pq.isEmpty()) {
Node node = pq.poll();
System.out.println( "Node ID: " + node.id
+ ", Cost: " + node.cost);
}
}
}
|
Python3
import queue
class Node:
def __init__( self , id , cost):
self . id = id
self .cost = cost
def __lt__( self , other):
return self .cost < other.cost
pq = queue.PriorityQueue()
pq.put(Node( 1 , 5 ))
pq.put(Node( 2 , 2 ))
pq.put(Node( 3 , 8 ))
pq.put(Node( 4 , 1 ))
while not pq.empty():
node = pq.get()
print ( "Node ID: {}, Cost: {}" . format (node. id , node.cost))
|
Javascript
class Node {
constructor(id, cost) {
this .id = id;
this .cost = cost;
}
}
const pq = [];
pq.push( new Node(1, 5));
pq.push( new Node(2, 2));
pq.push( new Node(3, 8));
pq.push( new Node(4, 1));
pq.sort((a, b) => a.cost - b.cost);
while (pq.length > 0) {
const node = pq.shift();
console.log(`Node ID: ${node.id}, Cost: ${node.cost}`);
}
|
C#
using System;
using System.Collections.Generic;
public class Node {
public int id;
public int cost;
public Node( int id, int cost)
{
this .id = id;
this .cost = cost;
}
}
public class NodeComparer : IComparer<Node> {
public int Compare(Node lhs, Node rhs)
{
return lhs.cost - rhs.cost;
}
}
public class Program {
static SortedSet<Node> pq
= new SortedSet<Node>( new NodeComparer());
public static void Main( string [] args)
{
pq.Add( new Node(1, 5));
pq.Add( new Node(2, 2));
pq.Add( new Node(3, 8));
pq.Add( new Node(4, 1));
while (pq.Count > 0) {
Node node = pq.Min;
pq.Remove(node);
Console.WriteLine( "Node ID: " + node.id
+ ", Cost: " + node.cost);
}
}
}
|
Output
Node ID: 4, Cost: 1
Node ID: 2, Cost: 2
Node ID: 1, Cost: 5
Node ID: 3, Cost: 8
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Alternatively, you could use a different data structure, such as a std::map or a std::unordered_map, to store the data for your shortest path search algorithm. These data structures allow you to store the data in a key-value format, where the key is the node ID and the value is the cost of the node. You can then iterate through the map and extract the nodes in the order that you desire.
Share your thoughts in the comments
Please Login to comment...