Browse Source

add topological sort

Olivier Marty 8 years ago
parent
commit
55ab8c5914
3 changed files with 26 additions and 2 deletions
  1. 0 2
      TODO
  2. 23 0
      code/TopologicalSort.cpp
  3. 3 0
      main.tex

+ 0 - 2
TODO

@@ -3,8 +3,6 @@ regarder le fichier de christoph fuch_bien.pdf (RMQ, bigint, ... plein de choses
 https://sites.google.com/site/indy256/algo/fenwick_tree
 Big integer
 Faire des algos de flots et de matching plus courts
-z-function
-Hopcroft Karp
 
 
 Liste des problèmes

+ 23 - 0
code/TopologicalSort.cpp

@@ -0,0 +1,23 @@
+//
+struct edge { int v, next; };
+bool TopSortDFS(int v, edge e[], int g[], int u[], int &m) {
+  u[v] = -2;
+  for(int i = g[v]; i != -1; i=e[i].next)
+    if(u[e[i].v]==-2)
+      return false;
+    else if(u[e[i].v]==-1 && !TopSortDFS(e[i].v, e, g, u, m))
+      return false;
+    u[v] = --m;
+    return true;
+}
+bool TopSort(edge e[], int g[], int n, int list[]) {
+  static int u[maxn];
+  int m = n;
+  memset(u, 255, sizeof(u));
+  for(int i = 0; i < n; i++)
+    if(u[i] == -1 && !TopSortDFS(i,e,g,u,m))
+      return false;
+  for(int i = 0; i < n; i++)
+    list[u[i]] = i;
+  return true;
+}

+ 3 - 0
main.tex

@@ -147,6 +147,9 @@ Temps de cuisson : $O(n)$
 \subsection{Bellman-Ford}
 {\scriptsize\lstinputlisting{code/BellmanFord.cpp}} % OK
 
+\subsection{Topological Sort}
+{\scriptsize\lstinputlisting{code/TopologicalSort.cpp}}
+
 \section{Structures de données}
 
 \subsection{Suffix arrays}