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

Class MarkovChainGraphUndirectedUnweighted

graphs/markov_chain.py:7–38  ·  view source on GitHub ↗

Undirected Unweighted Graph for running Markov Chain Algorithm

Source from the content-addressed store, hash-verified

5
6
7class MarkovChainGraphUndirectedUnweighted:
8 """
9 Undirected Unweighted Graph for running Markov Chain Algorithm
10 """
11
12 def __init__(self):
13 self.connections = {}
14
15 def add_node(self, node: str) -> None:
16 self.connections[node] = {}
17
18 def add_transition_probability(
19 self, node1: str, node2: str, probability: float
20 ) -> None:
21 if node1 not in self.connections:
22 self.add_node(node1)
23 if node2 not in self.connections:
24 self.add_node(node2)
25 self.connections[node1][node2] = probability
26
27 def get_nodes(self) -> list[str]:
28 return list(self.connections)
29
30 def transition(self, node: str) -> str:
31 current_probability = 0
32 random_value = random()
33
34 for dest in self.connections[node]:
35 current_probability += self.connections[node][dest]
36 if current_probability > random_value:
37 return dest
38 return ""
39
40
41def get_transitions(

Callers 1

get_transitionsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected