- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Path with Smallest Product of Edges with Weight >= 1
To discover the way with the smallest item of edges with weights more noteworthy than or breaking even with 1, we are able to utilise Dijkstra's calculation with a slight alteration. At first, we relegate a weight of 1 to the source hub and infinity to all other hubs. During the calculation execution, rather than upgrading the separate with the whole weights, we upgrade it with the item weights. This guarantees that the way with the least amount of weight is chosen. By selecting the minimum−weighted hub at each step, we iteratively discover the most brief way until we reach the goal hub. At long last, the item of weight along this way will be the smallest conceivable, fulfilling the given condition.
Methods Used
Modified Dijkstra's Algorithm with Weighted Product
Modified Bellman−Ford Algorithm with Weighted Product
Modified Dijkstra's Algorithm with Weighted Product
Within the modified Dijkstra's calculation with weighted items, we start by setting the weight of the source hub to boundlessness and the weight of all other hubs to boundlessness. During the execution of the calculation, rather than upgrading the separations with the entirety of weights, we overhauled them with the item of weights experienced so far. At each step, we select the hub with the minimum−weighted item and upgrade the weights of its neighbouring hubs in a similar manner. This preparation proceeds until we reach the goal hub. Eventually, the item of weights along this way will speak to the littlest conceivable, fulfilling the condition of weights being more prominent than or rising to 1.
Algorithm
Initialise all hub weights to interminability, except for the source hub, which is set to 0.
Create a purge set to keep track of gone−to nodes.
While there are unvisited nodes,
Select the hub with the least weighted item among the unvisited nodes.
Stamp the chosen hub as visited.
For each neighbouring hub not, however, visited:
-
Calculate the weight of the current hub and the weight of the edge interfacing them.
In the event that the calculated item is less than the weight of the neighbouring hub, replace its weight with the calculated product.
Repeat step 3 until the goal hub is gone or all hubs have been visited.
The weight of the goal hub speaks to the littlest item of weight along the way from the source to the goal.
Example
#include <bits/stdc++.h> using namespace std; // Function to return the smallest product of edges double modifiedDijkstra(int source, int destination, vector<vector<pair<int, double> > > graph) { // If the source is equal to the destination if (source == destination) return 0; // Initialize the priority queue set<pair<double, int>> pq; pq.insert({1, source}); // Visited array bool visited[graph.size()] = {0}; // While the priority queue is not empty while (!pq.empty()) { // Current node int current = pq.begin()->second; // Current product of weights double product = pq.begin()->first; // Pop the top-most element pq.erase(pq.begin()); // If already visited, continue if (visited[current]) continue; // Mark the node as visited visited[current] = true; // If it is a destination node if (current == destination) return product; // Traverse the neighbors of the current node for (auto neighbor : graph[current]) { int neighborNode = neighbor.first; double weight = neighbor.second; // Calculate the product of weights double newProduct = product * weight; // Insert the new product and neighbor into the priority queue pq.insert({newProduct, neighborNode}); } } // If no path exists return -1; } // Function to add an edge to the graph void addEdge(vector<vector<pair<int, double>>>& graph, int u, int v, double weight) { graph[u].push_back({v, weight}); } // Function to print the graph void printGraph(const vector<vector<pair<int, double>>>& graph) { for (int i = 1; i < graph.size(); i++) { cout << "Node " << i << ": "; for (auto neighbor : graph[i]) { cout << "(" << neighbor.first << ", " << neighbor.second << ") "; } cout << endl; } } // Driver code int main() { int numNodes = 3; // Graph as adjacency list vector<vector<pair<int, double>>> graph(numNodes + 1); // Input edges addEdge(graph, 1, 3, 9); addEdge(graph, 2, 3, 1); addEdge(graph, 1, 2, 5); // Source and destination int source = 1, destination = 3; // Modified Dijkstra double smallestProduct = modifiedDijkstra(source, destination, graph); // Print the result cout << "Smallest product of weights: " << smallestProduct << endl; // Print the graph cout << "Graph: " << endl; printGraph(graph); return 0; }
Output
Smallest product of weights: 5 Graph: Node 1: (3, 9) (2, 5) Node 2: (3, 1) Node 3:
Modified Bellman−Ford Algorithm with Weighted Product
Within the adjusted Bellman−Ford calculation with weighted object, we start via way of means of placing the load of the supply hub to at least one and the load of all different hubs to interminability. Amid every cycle, we unwind all the rims via way of means of evaluating the object of the modern node's weight and the load of the threshold interfacing them to the intention hub. In case the calculated object is littler than the load of the intention hub, we improve its weight with the calculated object. Rehash this deal with V−1 times, wherein V is the whole wide variety of hubs, to assure all plausible methods are considered. The weight of the intention hub speaks to the littlest object of weights alongside a manner from the supply to the intention.
Algorithm
All other hubs should have infinity weight, except for the source hub.
Repeat the taking after step V−1, where V is the entire number of nodes:
-
For each edge within the chart, calculate the item weight of the current hub and the weight of the edge interfacing them.
In the event that the calculated item is less than the weight of the goal hub, upgrade its weight with the calculated product.
After V−1 cycles, the weights of all hubs will be finalised.
During the calculation, an extra cycle will be distinguished if there is a negative−weight cycle within the chart. In case any weight is overhauled in this emphasis, it demonstrates the nearness of a negative−weight cycle.
The weight of the goal hub speaks to the littlest item of weight along the way from the source to the goal.
The Eager Colouring calculation works on the principle of allotting colours to vertices in a ravenous way, based on the accessible colours and the colours utilised by neighbouring vertices. While it may not continuously abdicate the least number of colours required for the chart, it gives a fast and efficient approach for vertex colouring.
Example
#include <iostream> #include <vector> #include <limits> struct Edge { int source, destination; double weight; }; // Function to find the smallest product of weights using the modified Bellman-Ford algorithm double findSmallestProduct(int numNodes, int source, int destination, std::vector<Edge>& edges) { std::vector<double> weights(numNodes, std::numeric_limits<double>::infinity()); weights[source] = 1; for (int i = 1; i < numNodes; i++) { for (const auto& edge : edges) { double newWeight = weights[edge.source] * edge.weight; if (newWeight < weights[edge.destination]) { weights[edge.destination] = newWeight; } } } for (const auto& edge : edges) { double newWeight = weights[edge.source] * edge.weight; if (newWeight < weights[edge.destination]) { return -1.0; // Negative-weight cycle detected } } return weights[destination]; } int main() { int numNodes = 4; std::vector<Edge> edges = { {0, 1, 2.0}, {1, 2, 0.5}, {2, 3, 1.5}, {0, 3, 1.2}, {1, 3, 0.8} }; int source = 0, destination = 3; double smallestProduct = findSmallestProduct(numNodes, source, destination, edges); if (smallestProduct < std::numeric_limits<double>::infinity()) { std::cout << "The smallest product of weights along the path from node " << source << " to node " << destination << " is: " << smallestProduct << std::endl; } else { std::cout << "A negative-weight cycle is detected. No valid path exists." << std::endl; } return 0; }
Output
The smallest product of weights along the path from node 0 to node 3 is: 1.2
Conclusion
This article clarifies how to discover the way with the littlest item of edges with weights more noteworthy than or rising to 1. It presents two calculations, the adjusted Dijkstra's calculation and the altered Bellman−Ford calculation, for understanding this issue. The adjusted Dijkstra's calculation chooses the hub with the least weighted item at each step, whereas the altered Bellman−Ford calculation iteratively unwinds the edges to overhaul the weights. The article gives the execution of both calculations in C and illustrates their utilisation with test inputs. The yield appears as the littlest item of weights along the way from the source to the goal hub.