app/graph/sorting.py
author Sverre Rabbelier <srabbelier@gmail.com>
Wed, 26 Nov 2008 23:56:19 +0000
changeset 594 06c2228e39cb
permissions -rw-r--r--
Added the python-graph module http://code.google.com/p/python-graph/ Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
594
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
# Copyright (c) 2007-2008 Pedro Matiello <pmatiello@gmail.com>
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Permission is hereby granted, free of charge, to any person
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
# obtaining a copy of this software and associated documentation
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# files (the "Software"), to deal in the Software without
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# restriction, including without limitation the rights to use,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# copy, modify, merge, publish, distribute, sublicense, and/or sell
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
# copies of the Software, and to permit persons to whom the
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
# Software is furnished to do so, subject to the following
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
# conditions:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# The above copyright notice and this permission notice shall be
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# included in all copies or substantial portions of the Software.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
# OTHER DEALINGS IN THE SOFTWARE.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
Sorting algorithms for python-graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
@sort: topological_sorting
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
# Topological sorting
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
def topological_sorting(graph):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
	"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
	Topological sorting.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
	@attention: Topological sorting is meaningful only for directed acyclic graphs.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
	@type  graph: graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
	@param graph: Graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
	@rtype:  list
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
	@return: Topological sorting for the graph.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
	"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
	# The topological sorting of a DAG is equivalent to its reverse postordering.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
	tmp, tmp, post = graph.depth_first_search()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
	post.reverse()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
	return post