Skip to content

Commit

Permalink
Refactored solutions 261 - 262
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Jan 31, 2021
1 parent ca8b139 commit 5cfcce7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
20 changes: 12 additions & 8 deletions Solutions/261.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@
determine a mapping between characters and their encoded binary strings.
"""

from typing import Dict
from typing import Dict, Union

# local import from the DataStructure module
from DataStructures.Tree import Node


def huffman_code_tree(
node: Node, left: bool = True, binString: str = ""
) -> Dict[str, str]:
# function implementing huffman coding
def huffman_code_tree(node: Union[Node, str], binString: str = "") -> Dict[str, str]:
if type(node) is str:
return {node: binString}
d = dict()
d.update(huffman_code_tree(node.left, True, binString + "0"))
d.update(huffman_code_tree(node.right, False, binString + "1"))
d.update(huffman_code_tree(node.left, binString + "0"))
d.update(huffman_code_tree(node.right, binString + "1"))
return d


Expand All @@ -57,3 +53,11 @@ def get_huffman_code(char_freq: Dict[str, int]) -> Dict[str, str]:

if __name__ == "__main__":
print(get_huffman_code({"c": 1, "a": 2, "t": 2, "s": 1}))


"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""
47 changes: 19 additions & 28 deletions Solutions/262.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,29 @@
"""

from sys import maxsize
from typing import Dict, List, Optional, Tuple
from typing import Dict, List, Optional, Set, Tuple

# local import from the DataStructure module
from DataStructures.Graph import GraphUndirectedUnweighted


def get_bridges_helper(
self,
graph: GraphUndirectedUnweighted,
node: int,
visited: set,
visited: Set[int],
parent: Dict[int, Optional[int]],
low: Dict[int, int],
disc: Dict[int, int],
bridges: List[Tuple[int, int]],
) -> None:
# DFS based helper function to find all bridges
# find all bridges using dfs
visited.add(node)
disc[node] = self.time
low[node] = self.time
self.time += 1
for neighbour in self.connections[node]:
disc[node] = graph.time
low[node] = graph.time
graph.time += 1
for neighbour in graph.connections[node]:
if neighbour not in visited:
parent[neighbour] = node
self.get_bridges_helper(neighbour, visited, parent, low, disc, bridges)
get_bridges_helper(graph, neighbour, visited, parent, low, disc, bridges)
# check if the subtree rooted with neighbour has a connection to one of the
# ancestors of node
low[node] = min(low[node], low[neighbour])
Expand All @@ -38,30 +37,22 @@ def get_bridges_helper(
if low[neighbour] > disc[node]:
bridges.append((node, neighbour))
elif neighbour != parent[node]:
# update low value of node for parent function calls.
low[node] = min(low[node], disc[neighbour])


def get_bridges(self) -> List[Tuple[int, int]]:
# function to get all the bridges in a graph
def get_bridges(graph: GraphUndirectedUnweighted) -> List[Tuple[int, int]]:
visited = set()
disc = {node: maxsize for node in self.connections}
low = {node: maxsize for node in self.connections}
parent = {node: None for node in self.connections}
disc = {node: maxsize for node in graph.connections}
low = {node: maxsize for node in graph.connections}
parent = {node: None for node in graph.connections}
bridges = []
self.time = 0
for node in self.connections:
graph.time = 0
for node in graph.connections:
if node not in visited:
self.get_bridges_helper(node, visited, parent, low, disc, bridges)
get_bridges_helper(graph, node, visited, parent, low, disc, bridges)
return bridges


# adding the required methods and attributes to the graph class
setattr(GraphUndirectedUnweighted, "get_bridges_helper", get_bridges_helper)
setattr(GraphUndirectedUnweighted, "get_bridges", get_bridges)
setattr(GraphUndirectedUnweighted, "time", 0)


if __name__ == "__main__":
g1 = GraphUndirectedUnweighted()
g1.add_edge(1, 0)
Expand All @@ -70,14 +61,14 @@ def get_bridges(self) -> List[Tuple[int, int]]:
g1.add_edge(0, 3)
g1.add_edge(3, 4)
print("Bridges in first graph:")
print(*g1.get_bridges())
print(*get_bridges(g1))

g2 = GraphUndirectedUnweighted()
g2.add_edge(0, 1)
g2.add_edge(1, 2)
g2.add_edge(2, 3)
print("\nBridges in second graph:")
print(*g2.get_bridges())
print(*get_bridges(g2))

g3 = GraphUndirectedUnweighted()
g3.add_edge(0, 1)
Expand All @@ -89,4 +80,4 @@ def get_bridges(self) -> List[Tuple[int, int]]:
g3.add_edge(3, 5)
g3.add_edge(4, 5)
print("\nBridges in third graph:")
print(*g3.get_bridges())
print(*get_bridges(g3))

0 comments on commit 5cfcce7

Please sign in to comment.