Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Suppose we have a Bipartite graph. If you want each node to have degree 0, 1 or 2, one way to do this would be the following. If you want to do a <em>matching</em>, either look up the algorithm (I don't remember it), or change maxDegree to 1 and I think it should work as a matching instead. Regardless, let me know if this doesn't do what you want.</p> <pre><code>def hamiltPath(graph): """This partitions a bipartite graph into a set of components with each component consisting of a hamiltonian path.""" # The maximum degree maxDegree = 2 # Get all the nodes. We will process each of these. remaining = graph.vertices() # Create a new empty graph to which we will add pairs of nodes. newGraph = Graph() # Loop while there's a remaining vertex. while len(remaining) &gt; 0: # Get the next arbitrary vertex. node = remaining.pop() # Now get its neighbors that are in the remaining set. neighbors = [n for n in graph.neighbors(node) if n in remaining] # If this list of neighbors is non empty, then add (node, neighbors[0]) # to the new graph. if len(neighbors) &gt; 0: # If this is not an optimal algorithm, I suspect the selection # a vertex in this indexing step is the crux. Improve this # selection and the algorthim might be optimized, if it isn't # already (optimized in result not time or space complexity). neighbor = neighbors[0] newGraph.addEdge(node, neighbor) # "node" has already been removed from the remaining vertices. # We need to remove "neighbor" if its degree is too high. if len(newGraph.neighbors(neighbor)) &gt;= maxDegree: remaining.remove(neighbor) return newGraph class Graph: """A graph that is represented by pairs of vertices. This was created For conciseness, not efficiency""" def __init__(self): self.graph = set() def addEdge(self, a, b): """Adds the vertex (a, b) to the graph""" self.graph = self.graph.union({(a, b)}) def neighbors(self, node): """Returns all of the neighbors of a as a set. This is safe to modify.""" return (set(a[0] for a in self.graph if a[1] == node). union( set(a[1] for a in self.graph if a[0] == node) )) def vertices(self): """Returns a set of all of the vertices. This is safe to modify.""" return (set(a[1] for a in self.graph). union( set(a[0] for a in self.graph) )) def __repr__(self): result = "\n" for (a, b) in self.graph: result += str(a) + "," + str(b) + "\n" # Remove the leading and trailing white space. result = result[1:-1] return result graph = Graph() graph.addEdge("0", "4") graph.addEdge("1", "8") graph.addEdge("2", "8") graph.addEdge("3", "5") graph.addEdge("3", "6") graph.addEdge("3", "7") graph.addEdge("3", "8") graph.addEdge("3", "9") graph.addEdge("3", "10") graph.addEdge("3", "11") print(graph) print() print(hamiltPath(graph)) # Result of this is: # 10,3 # 1,8 # 2,8 # 11,3 # 0,4 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload