Skip to content

Commit

Permalink
Refactored solutions 255 - 256
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Jan 31, 2021
1 parent 3016499 commit d724510
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
40 changes: 30 additions & 10 deletions Solutions/255.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""
Problem:
The transitive closure of a graph is a measure of which vertices are reachable from other vertices. It can be represented as a matrix M, where M[i][j] == 1 if there is a path between vertices i and j, and otherwise 0.
The transitive closure of a graph is a measure of which vertices are reachable from
other vertices. It can be represented as a matrix M, where M[i][j] == 1 if there is a
path between vertices i and j, and otherwise 0.
For example, suppose we are given the following graph in adjacency list form:
Expand All @@ -21,26 +23,44 @@
"""


def get_transitive_helper(origin, curr_node, graph, transitive_matrix, visited):
from typing import List, Set


def get_transitive_matrix_helper(
origin: int,
curr_node: int,
graph: List[List[int]],
transitive_matrix: List[List[int]],
visited: Set[int],
) -> None:
# helper function to generate the transitive matrix using dfs
for node in graph[curr_node]:
transitive_matrix[origin][node] = 1
if node not in visited:
visited.add(node)
get_transitive_helper(origin, node, graph, transitive_matrix, visited)
get_transitive_matrix_helper(
origin, node, graph, transitive_matrix, visited
)


def get_transitive(graph):
def get_transitive_matrix(graph: List[List[int]]) -> List[List[int]]:
num_nodes = len(graph)
# creating and updating the transitive matrix
transitive_matrix = [[0 for _ in range(num_nodes)] for _ in range(num_nodes)]
for node in range(num_nodes):
get_transitive_helper(node, node, graph, transitive_matrix, set([node]))
get_transitive_matrix_helper(node, node, graph, transitive_matrix, set([node]))
return transitive_matrix


# DRIVER CODE
graph = [[0, 1, 3], [1, 2], [2], [3]]
if __name__ == "__main__":
graph = [[0, 1, 3], [1, 2], [2], [3]]

for row in get_transitive_matrix(graph):
print(*row)


for row in get_transitive(graph):
print(*row)
"""
SPECS:
TIME COMPLEXITY: O(n ^ 2)
SPACE COMPLEXITY: O(n ^ 2)
"""
44 changes: 24 additions & 20 deletions Solutions/256.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
"""
Problem:
Given a linked list, rearrange the node values such that they appear in alternating low -> high -> low -> high ... form. For example, given 1 -> 2 -> 3 -> 4 -> 5, you should return 1 -> 3 -> 2 -> 5 -> 4.
Given a linked list, rearrange the node values such that they appear in alternating
low -> high -> low -> high ... form. For example, given 1 -> 2 -> 3 -> 4 -> 5, you
should return 1 -> 3 -> 2 -> 5 -> 4.
"""

from DataStructures.LinkedList import Node, Linked_list
from DataStructures.LinkedList import Node, LinkedList


def rearrange(self):
# getting the nodes in sorted format
nodes = [int(node) for node in self]
def rearrange(ll: LinkedList) -> None:
nodes_count = len(ll)
nodes = [int(node) for node in ll]
nodes.sort()
# interleaving the nodes to form alternating pattern (low -> high -> low ...)
for i in range(2, len(nodes), 2):

for i in range(2, nodes_count, 2):
nodes[i], nodes[i - 1] = nodes[i - 1], nodes[i]
# updating the linked list
curr = self.head
for i in range(len(nodes)):

curr = ll.head
for i in range(nodes_count):
curr.val = nodes[i]
curr = curr.next


# adding the rearrange method to the class
setattr(Linked_list, "rearrange", rearrange)

if __name__ == "__main__":
LL = LinkedList()

# DRIVER CODE
LL = Linked_list()
for i in range(1, 6):
LL.add(i)

for i in range(1, 6):
LL.add(i)
print(LL)
rearrange(LL)
print(LL)

print(LL)

LL.rearrange()
"""
SPECS:
print(LL)
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""

0 comments on commit d724510

Please sign in to comment.