Skip to content

Commit 16b19fd

Browse files
author
Hamid Gasmi
committed
Issue #148 is completed with an iterative DFS
1 parent 3aca5a6 commit 16b19fd

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

4-np-complete-problems/2-coping_with_np_completeness/2sat_circuit_design.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import sys
22
from collections import deque
33

4+
# This problem can be reduced to a 2-SAT problem
5+
# 2-SAT problem can be then solved efficiently by using an implication graph
6+
# It's solved 3 different ways:
7+
# ... 1- Naive solution
8+
# ... 2- Implication graph + recursive DFS
9+
# ... 3- Implication graph + Iterative DFS
10+
411
class Implication_graph:
512

613
def __init__(self, n, clauses):
@@ -109,14 +116,14 @@ def dfs(self, an_adjacency_list, dfs_preorder, dfs_postorder, iterative):
109116
else:
110117
self.explore(v, an_adjacency_list, dfs_preorder, dfs_postorder)
111118

112-
def stronly_connected_components(self):
119+
def stronly_connected_components(self, iterative):
113120

114121
dfs_postorder = []
115122

116123
# 1. Find postorder in Gr
117124
# ... Source vertices in Gr have the highest postorder
118125
# ... A source vertice in Gr is Sink vertice in G
119-
self.dfs(self.reversed_adjacency_list, None, dfs_postorder, iterative = True)
126+
self.dfs(self.reversed_adjacency_list, None, dfs_postorder, iterative)
120127

121128
# 2. Find SCCs in G
122129
sccs = []
@@ -125,13 +132,16 @@ def stronly_connected_components(self):
125132
v = dfs_postorder[i]
126133
if not self.visited[v]:
127134
sccs.append([])
128-
self.explore_iterative(v, self.adjacency_list, None, sccs[len(sccs) - 1])
135+
if iterative:
136+
self.explore_iterative(v, self.adjacency_list, None, sccs[len(sccs) - 1])
137+
else:
138+
self.explore(v, self.adjacency_list, None, sccs[len(sccs) - 1])
129139

130140
return sccs
131141

132-
def solve_2sat(self):
142+
def solve_2sat(self, iterative):
133143

134-
strongly_connected_components = self.stronly_connected_components()
144+
strongly_connected_components = self.stronly_connected_components(iterative)
135145

136146
# sccs is ordered in reversed topological order
137147
vertice_assignment = [-1] * self.vertices_count
@@ -159,11 +169,11 @@ def __init__(self, vertice, adjacent_index):
159169
self.vertice = vertice
160170
self.adjacent_index = adjacent_index
161171

162-
def isSatisfiable(n, clauses):
172+
def isSatisfiable(n, clauses, iterative):
163173

164174
g = Implication_graph(n, clauses)
165175

166-
return g.solve_2sat()
176+
return g.solve_2sat(iterative)
167177

168178
# Naive Solution:
169179
# Time Complexity: O(2^n)
@@ -190,9 +200,9 @@ def isSatisfiable_naive(n, clauses):
190200

191201
n, m = map(int, input().split())
192202
clauses = [ list(map(int, input().split())) for i in range(m) ]
193-
203+
194204
#result = isSatisfiable_naive(n, clauses)
195-
result = isSatisfiable(n, clauses)
205+
result = isSatisfiable(n, clauses, iterative=True)
196206

197207
if result is None:
198208
print("UNSATISFIABLE")

0 commit comments

Comments
 (0)