comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Topological Sort Help
Date: Sun, 04 Feb 2007 22:45:50 +0100
Date: 2007-02-04T22:45:50+01:00	[thread overview]
Message-ID: <87sldllhpt.fsf@ludovic-brenta.org> (raw)
In-Reply-To: 1170618852.885855.258390@j27g2000cwj.googlegroups.com

isaac2004 writes:
> hello i am trying to further my knowledge of graphs and i keep running
> into this function called topological sort. i read the definition and
> somewhat understand it as a function that does the following
>
> 1. searches for a directed cycle
> 2. sorts vertices of a graph in order onto a stack or array of some
> type so the nodes can be popped in order
>
> so my questions are
>
> are these reasonable thoughts on this algorithm

Have you read [1] http://en.wikipedia.org/wiki/Topological_sort ?

The first algorithm explained works the other way: it sorts the nodes
first, and removes edges from the graph as it goes.  If there are
edges remaining after the sort, then the graph has a cycle and cannot
be sorted.

In fact, how are you going to find cycles in a graph without doing a
topological sort? :)

> how would i write up an algorithm of this nature, would it be just
> like traversing down a list and adding elements to a stack. thanks
> for any help

The first step is to devise a representation of the graph as a data
structure.  In a directed graph, there are nodes and edges; each edge
links two nodes and has a direction. One naive way of representing
such a graph is with a two-dimensional array of Booleans, where
Graph(X, Y) is True iff there is an edge from X to Y.  But that
representation does not scale well to many nodes, so you may have to
create a more memory-efficient data structure if your problem warrants
that.

I suggest you start with the naive representation first, write your
algorithm, and then change the data structure if you run into memory
problems.

HTH

-- 
Ludovic Brenta.



  reply	other threads:[~2007-02-04 21:45 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-04 19:54 Topological Sort Help isaac2004
2007-02-04 21:45 ` Ludovic Brenta [this message]
2007-02-05 20:30   ` isaac2004
2007-02-05 20:39     ` Ludovic Brenta
2007-02-06  2:18       ` isaac2004
2007-02-06  9:06         ` Ludovic Brenta
2007-02-08  1:19           ` isaac2004
2007-02-08  9:25             ` Ludovic Brenta
2007-02-08 18:14               ` isaac2004
2007-02-08 18:24                 ` Ludovic Brenta
2007-02-08 18:29                   ` isaac2004
2007-02-08 18:54                     ` Ludovic Brenta
2007-02-08 19:14                       ` isaac2004
2007-02-08 19:27                         ` Ludovic Brenta
2007-02-08 20:22                           ` isaac2004
2007-02-09  2:04                             ` isaac2004
2007-02-08 18:38             ` Jeffrey R. Carter
2007-02-08 18:53               ` isaac2004
2007-02-09  4:57                 ` Jeffrey R. Carter
2007-02-10  8:37 ` s053914
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox