MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / valid_connection

Function valid_connection

backtracking/hamiltonian_cycle.py:11–46  ·  view source on GitHub ↗

Checks whether it is possible to add next into path by validating 2 statements 1. There should be path between current and next vertex 2. Next vertex should not be in path If both validations succeed we return True, saying that it is possible to connect this vertices, otherwise

(
    graph: list[list[int]], next_ver: int, curr_ind: int, path: list[int]
)

Source from the content-addressed store, hash-verified

9
10
11def valid_connection(
12 graph: list[list[int]], next_ver: int, curr_ind: int, path: list[int]
13) -> bool:
14 """
15 Checks whether it is possible to add next into path by validating 2 statements
16 1. There should be path between current and next vertex
17 2. Next vertex should not be in path
18 If both validations succeed we return True, saying that it is possible to connect
19 this vertices, otherwise we return False
20
21 Case 1:Use exact graph as in main function, with initialized values
22 >>> graph = [[0, 1, 0, 1, 0],
23 ... [1, 0, 1, 1, 1],
24 ... [0, 1, 0, 0, 1],
25 ... [1, 1, 0, 0, 1],
26 ... [0, 1, 1, 1, 0]]
27 >>> path = [0, -1, -1, -1, -1, 0]
28 >>> curr_ind = 1
29 >>> next_ver = 1
30 >>> valid_connection(graph, next_ver, curr_ind, path)
31 True
32
33 Case 2: Same graph, but trying to connect to node that is already in path
34 >>> path = [0, 1, 2, 4, -1, 0]
35 >>> curr_ind = 4
36 >>> next_ver = 1
37 >>> valid_connection(graph, next_ver, curr_ind, path)
38 False
39 """
40
41 # 1. Validate that path exists between current and next vertices
42 if graph[path[curr_ind - 1]][next_ver] == 0:
43 return False
44
45 # 2. Validate that next vertex is not already in path
46 return not any(vertex == next_ver for vertex in path)
47
48
49def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int) -> bool:

Callers 1

util_hamilton_cycleFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected