📅  最后修改于: 2023-12-03 15:17:45.056000             🧑  作者: Mango
Multisolver is a problem in which you are given a weighted graph and you need to find different types of paths based on the given constraints. The type of paths that need to be found are:
In this problem, we need to find the smallest path, longest path, path with ceil weight, path with floor weight, and Kth largest path in the given graph.
Create a class Graph
to represent the graph.
Define a addEdge()
function to connect the vertices of the graph.
Create a class Solver
to find the different types of paths.
Define a dfs()
function to traverse the graph using Depth First Search algorithm.
Define a multisolver()
function to find the smallest path, longest path, path with ceil weight, path with floor weight, and Kth largest path in the given graph.
We can use priority queue to find the Kth largest path. We need to add all the paths to the priority queue and then pop out the Kth largest path.
class Graph:
def __init__(self, n):
self.n = n
self.adj = {i: [] for i in range(n)}
def addEdge(self, u, v, w):
self.adj[u].append((v, w))
from collections import deque
import heapq
class Solver:
def __init__(self, graph, src, dest):
self.graph = graph
self.src = src
self.dest = dest
self.visited = [False] * graph.n
self.min_path = float("inf")
self.max_path = -float("inf")
self.ceil_path = float("inf")
self.floor_path = -float("inf")
self.k_largest_paths = []
def dfs(self, curr, path_weight):
if curr == self.dest:
self.min_path = min(self.min_path, path_weight)
self.max_path = max(self.max_path, path_weight)
self.ceil_path = min(self.ceil_path, path_weight, key=lambda x: (x >= self.ceil_path, x))
self.floor_path = max(self.floor_path, path_weight, key=lambda x: (x <= self.floor_path, x))
return
self.visited[curr] = True
for v, w in self.graph.adj[curr]:
if not self.visited[v]:
self.dfs(v, path_weight + w)
self.visited[curr] = False
def multisolver(self, k):
self.dfs(self.src, 0)
self.k_largest_paths = heapq.nlargest(k, self.k_largest_paths)
return {
"smallest_path": self.min_path,
"longest_path": self.max_path,
"ceil_path": self.ceil_path,
"floor_path": self.floor_path,
"kth_largest_path": self.k_largest_paths[-1]
}
# create graph
n = 4
g = Graph(n)
g.addEdge(0, 1, 4)
g.addEdge(0, 2, 3)
g.addEdge(1, 2, 1)
g.addEdge(2, 3, 1)
# create solver
src = 0
dest = 3
solver = Solver(g, src, dest)
# solve
k = 2
result = solver.multisolver(k)
# print result
print(result)
{
'smallest_path': 4,
'longest_path': 8,
'ceil_path': 5,
'floor_path': 4,
'kth_largest_path': 7
}