MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / find_unit_clauses

Function find_unit_clauses

other/davis_putnam_logemann_loveland.py:235–282  ·  view source on GitHub ↗

Returns the unit symbols and their values to satisfy clause. Unit symbols are symbols in a formula that are: - Either the only symbol in a clause - Or all other literals in that clause have been assigned ``False`` This has the following steps: 1. Find symbols that ar

(
    clauses: list[Clause],
    model: dict[str, bool | None],  # noqa: ARG001
)

Source from the content-addressed store, hash-verified

233
234
235def find_unit_clauses(
236 clauses: list[Clause],
237 model: dict[str, bool | None], # noqa: ARG001
238) -> tuple[list[str], dict[str, bool | None]]:
239 """
240 Returns the unit symbols and their values to satisfy clause.
241
242 Unit symbols are symbols in a formula that are:
243 - Either the only symbol in a clause
244 - Or all other literals in that clause have been assigned ``False``
245
246 This has the following steps:
247 1. Find symbols that are the only occurrences in a clause.
248 2. Find symbols in a clause where all other literals are assigned ``False``.
249 3. Assign ``True`` or ``False`` depending on whether the symbols occurs in
250 normal or complemented form respectively.
251
252 >>> clause1 = Clause(["A4", "A3", "A5'", "A1", "A3'"])
253 >>> clause2 = Clause(["A4"])
254 >>> clause3 = Clause(["A3"])
255 >>> clauses, symbols = generate_parameters(Formula([clause1, clause2, clause3]))
256 >>> unit_clauses, values = find_unit_clauses(clauses, {})
257 >>> unit_clauses
258 ['A4', 'A3']
259 >>> values
260 {'A4': True, 'A3': True}
261 """
262 unit_symbols = []
263 for clause in clauses:
264 if len(clause) == 1:
265 unit_symbols.append(next(iter(clause.literals.keys())))
266 else:
267 f_count, n_count = 0, 0
268 for literal, value in clause.literals.items():
269 if value is False:
270 f_count += 1
271 elif value is None:
272 sym = literal
273 n_count += 1
274 if f_count == len(clause) - 1 and n_count == 1:
275 unit_symbols.append(sym)
276 assignment: dict[str, bool | None] = {}
277 for i in unit_symbols:
278 symbol = i[:2]
279 assignment[symbol] = len(i) == 2
280 unit_symbols = [i[:2] for i in unit_symbols]
281
282 return unit_symbols, assignment
283
284
285def dpll_algorithm(

Callers 1

dpll_algorithmFunction · 0.85

Calls 2

keysMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected