forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request matthewsamuel95#572 from shakib609/master
Refactor all python files to comply with PEP-8 style guide
- Loading branch information
Showing
38 changed files
with
799 additions
and
788 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,106 @@ | ||
class Graph(): | ||
def __init__(self, vertices): | ||
self.graph = [[0 for column in range(vertices)] | ||
for row in range(vertices)] | ||
self.V = vertices | ||
|
||
class Graph(): | ||
def __init__(self, vertices): | ||
self.graph = [[0 for column in range(vertices)]\ | ||
for row in range(vertices)] | ||
self.V = vertices | ||
|
||
def isSafe(self, v, pos, path): | ||
# Check if current vertex and last vertex | ||
# in path are adjacent | ||
if self.graph[ path[pos-1] ][v] == 0: | ||
def isSafe(self, v, pos, path): | ||
# Check if current vertex and last vertex | ||
# in path are adjacent | ||
if self.graph[path[pos - 1]][v] == 0: | ||
return False | ||
# Check if current vertex not already in path | ||
for vertex in path: | ||
if vertex == v: | ||
|
||
# Check if current vertex not already in path | ||
for vertex in path: | ||
if vertex == v: | ||
return False | ||
|
||
return True | ||
def hamCycleUtil(self, path, pos): | ||
# base case: if all vertices are | ||
# included in the path | ||
if pos == self.V: | ||
# Last vertex must be adjacent to the | ||
# first vertex in path to make a cyle | ||
if self.graph[ path[pos-1] ][ path[0] ] == 1: | ||
|
||
def hamCycleUtil(self, path, pos): | ||
|
||
# base case: if all vertices are | ||
# included in the path | ||
if pos == self.V: | ||
# Last vertex must be adjacent to the | ||
# first vertex in path to make a cyle | ||
if self.graph[path[pos - 1]][path[0]] == 1: | ||
return True | ||
else: | ||
else: | ||
return False | ||
for v in range(1,self.V): | ||
if self.isSafe(v, pos, path) == True: | ||
path[pos] = v | ||
if self.hamCycleUtil(path, pos+1) == True: | ||
|
||
for v in range(1, self.V): | ||
|
||
if self.isSafe(v, pos, path) is True: | ||
|
||
path[pos] = v | ||
|
||
if self.hamCycleUtil(path, pos + 1) is True: | ||
return True | ||
# Remove current vertex if it doesn't | ||
# lead to a solution | ||
|
||
# Remove current vertex if it doesn't | ||
# lead to a solution | ||
path[pos] = -1 | ||
|
||
return False | ||
|
||
def hamCycle(self): | ||
path = [-1] * self.V | ||
|
||
''' Let us put vertex 0 as the first vertex | ||
in the path. If there is a Hamiltonian Cycle, | ||
then the path can be started from any point | ||
|
||
def hamCycle(self): | ||
path = [-1] * self.V | ||
''' Let us put vertex 0 as the first vertex | ||
in the path. If there is a Hamiltonian Cycle, | ||
then the path can be started from any point | ||
of the cycle as the graph is undirected ''' | ||
path[0] = 0 | ||
if self.hamCycleUtil(path,1) == False: | ||
print "Solution does not exist\n" | ||
|
||
if self.hamCycleUtil(path, 1) is False: | ||
print("Solution does not exist\n") | ||
return False | ||
self.printSolution(path) | ||
|
||
self.printSolution(path) | ||
return True | ||
|
||
def printSolution(self, path): | ||
print "Solution Exists: Following is one Hamiltonian Cycle" | ||
for vertex in path: | ||
print vertex, | ||
print path[0], "\n" | ||
|
||
''' Let us create the following graph | ||
(0)--(1)--(2) | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
(3)-------(4) ''' | ||
g1 = Graph(5) | ||
g1.graph = [ [0, 1, 0, 1, 0], [1, 0, 1, 1, 1], | ||
[0, 1, 0, 0, 1,],[1, 1, 0, 0, 1], | ||
[0, 1, 1, 1, 0], ] | ||
|
||
# Print the solution | ||
g1.hamCycle(); | ||
|
||
''' Let us create the following graph | ||
(0)--(1)--(2) | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
(3) (4) ''' | ||
g2 = Graph(5) | ||
g2.graph = [ [0, 1, 0, 1, 0], [1, 0, 1, 1, 1], | ||
[0, 1, 0, 0, 1,], [1, 1, 0, 0, 0], | ||
[0, 1, 1, 0, 0], ] | ||
|
||
# Print the solution | ||
g2.hamCycle(); | ||
|
||
def printSolution(self, path): | ||
print("Solution Exists: Following is one Hamiltonian Cycle") | ||
for vertex in path: | ||
print(vertex) | ||
print(path[0], "\n") | ||
|
||
|
||
''' | ||
Let us create the following graph | ||
(0)--(1)--(2) | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
(3)-------(4) | ||
''' | ||
g1 = Graph(5) | ||
g1.graph = [ | ||
[0, 1, 0, 1, 0], | ||
[1, 0, 1, 1, 1], | ||
[0, 1, 0, 0, 1], | ||
[1, 1, 0, 0, 1], | ||
[0, 1, 1, 1, 0], | ||
] | ||
|
||
# Print the solution | ||
g1.hamCycle() | ||
''' | ||
Let us create the following graph | ||
(0)--(1)--(2) | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
(3) (4) | ||
''' | ||
g2 = Graph(5) | ||
g2.graph = [ | ||
[0, 1, 0, 1, 0], | ||
[1, 0, 1, 1, 1], | ||
[0, 1, 0, 0, 1], | ||
[1, 1, 0, 0, 0], | ||
[0, 1, 1, 0, 0], | ||
] | ||
|
||
# Print the solution | ||
g2.hamCycle() |
Oops, something went wrong.