-
Notifications
You must be signed in to change notification settings - Fork 0
/
175.py
78 lines (63 loc) · 1.93 KB
/
175.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Problem:
You are given a starting state start, a list of transition probabilities for a Markov
chain, and a number of steps num_steps. Run the Markov chain starting from start for
num_steps and compute the number of times we visited each state.
For example, given the starting state a, number of steps 5000, and the following
transition probabilities:
[
('a', 'a', 0.9),
('a', 'b', 0.075),
('a', 'c', 0.025),
('b', 'a', 0.15),
('b', 'b', 0.8),
('b', 'c', 0.05),
('c', 'a', 0.25),
('c', 'b', 0.25),
('c', 'c', 0.5)
]
One instance of running this Markov chain might produce
{'a': 3012, 'b': 1656, 'c': 332 }.
"""
from random import random
from typing import Dict, List, Tuple
from DataStructures.Graph import GraphDirectedWeighted
def get_transition_form_node(graph: GraphDirectedWeighted, node: str) -> str:
transition = random()
curr = 0
for neighbour in graph.connections[node]:
curr += graph.connections[node][neighbour]
if curr >= transition:
return neighbour
def get_transitions(
start: str, transitions: List[Tuple[str, str, float]], steps: int
) -> Dict[str, int]:
# generating graph
graph = GraphDirectedWeighted()
for (node1, node2, probability) in transitions:
graph.add_edge(node1, node2, probability)
# generating visited map
visited = {node: 0 for node in graph.connections}
node = start
for _ in range(steps):
node = get_transition_form_node(graph, node)
visited[node] += 1
return visited
if __name__ == "__main__":
transitions = [
("a", "a", 0.9),
("a", "b", 0.075),
("a", "c", 0.025),
("b", "a", 0.15),
("b", "b", 0.8),
("b", "c", 0.05),
("c", "a", 0.25),
("c", "b", 0.25),
("c", "c", 0.5),
]
print(get_transitions("a", transitions, 5000))
"""
SPECS:
TIME COMPLEXITY: O(steps + transition states)
SPACE COMPLEXITY: O(transition states)
"""