From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,cd5005cccfba0311 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.scarlet.biz!news.scarlet.biz.POSTED!not-for-mail NNTP-Posting-Date: Sun, 04 Feb 2007 15:45:53 -0600 From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Topological Sort Help References: <1170618852.885855.258390@j27g2000cwj.googlegroups.com> Date: Sun, 04 Feb 2007 22:45:50 +0100 Message-ID: <87sldllhpt.fsf@ludovic-brenta.org> User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:kFNGFsqrkfzw88KDxtdlteyNLzo= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii NNTP-Posting-Host: 62.235.232.27 X-Trace: sv3-fTME6etSXpJb92/XeoDjjeyC4OCvUvyi72b4+PAJRNWHwqZJQmkFF5tSmZKBncGPo9dvf8WISCnS2MT!CLdkmnVqT6783/PRt47vnD2scl6tK6ifLiTjFPAucvS0At8fVgp3mW77IlSaNl7aOUa2dPWdUw== X-Complaints-To: abuse@scarlet.be X-DMCA-Complaints-To: abuse@scarlet.biz X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news2.google.com comp.lang.ada:8935 Date: 2007-02-04T22:45:50+01:00 List-Id: 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.