00001 __copyright__ = """
00002 Copyright 2008 Sean Ross-Ross
00003 """
00004 __license__ = """
00005 This file is part of SLIMpy .
00006
00007 SLIMpy is free software: you can redistribute it and/or modify
00008 it under the terms of the GNU Lesser General Public License as published by
00009 the Free Software Foundation, either version 3 of the License, or
00010 (at your option) any later version.
00011
00012 SLIMpy is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015 GNU Lesser General Public License for more details.
00016
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00019 """
00020
00021 class queue(list):
00022 def push(self,item):
00023 self.insert(0,item)
00024
00025
00026 class DFS(object):
00027
00028 def __init__(self,G):
00029 self.colour = {}
00030 self.pi = {}
00031 self.d = {}
00032 self.time = 0
00033 self.f = {}
00034 self.G = G
00035 self.Top = []
00036
00037
00038 self.dfs()
00039
00040 def dfs(self):
00041
00042 for u in self.G:
00043 self.colour[u] = "white"
00044 self.pi[u] = None
00045
00046 self.time = 0
00047
00048 for u in self.G.getSourceNodes():
00049 if self.colour[u] == "white":
00050 self.dfsVisit(u)
00051
00052 def dfsVisit(self,u):
00053
00054 self.colour[u] = "grey"
00055 self.time += 1
00056 self.d[u] = self.time
00057
00058 for v in self.G.adj(u):
00059 if self.colour[v] == "white":
00060 self.pi[v] = u
00061 self.dfsVisit(v)
00062
00063 self.colour[u] = "black"
00064 self.f[u] = self.time = self.time+1
00065 self.Top.insert(0,u)
00066
00067 def printPath(self,s,v):
00068 if v == s:
00069 print s
00070 elif self.pi[v] == None:
00071 print "No path from %s to %s exists" %(s,v)
00072 else:
00073 self.printPath(s, self.pi[v])
00074 print v
00075
00076 def printTop(self):
00077 for u in self.Top:
00078 print u
00079
00080 def printDF(self):
00081 print " %13s | d | f " %("Node")
00082 for u in self.d.keys():
00083 print " %13s | %s | %s "%(u,self.d[u],self.f[u])
00084
00085
00086
00087 class BFS(object):
00088
00089
00090 def __init__(self,G,s):
00091 self.colour = {}
00092 self.d = {}
00093 self.pi = {}
00094 self.G = G
00095 self.bfs(s)
00096
00097
00098
00099 def bfs(self,s):
00100 Q = queue()
00101
00102 for u in self.G:
00103 self.colour[u] = "white"
00104 self.d[u] = None
00105 self.pi[u] = None
00106
00107 self.colour[s] = "grey"
00108 self.d[s] = 0
00109 self.pi[s] = None
00110 Q.push(s)
00111
00112 while len(Q) is not 0:
00113 u = Q.pop()
00114 print "u=",u,
00115 for v in self.G.adj(u):
00116 if self.colour[v] == "white":
00117 self.colour[v] == "grey"
00118 self.d[v] = self.d[u] + 1
00119 self.pi[v] = u
00120 Q.push(v)
00121 self.colour[u] = "black"
00122
00123 def printPath(self,s,v):
00124
00125 if v == s:
00126 print s
00127 elif self.pi[v] == None:
00128 print "No path from %s to %s exists" %(s,v)
00129 else:
00130 self.printPath(s, self.pi[v])
00131 print v
00132