KrisLibrary  1.0.0
DirectedGraph.h
1 #ifndef GRAPH_DIRECTED_GRAPH_H
2 #define GRAPH_DIRECTED_GRAPH_H
3 
4 #include "Graph.h"
5 #include "Callback.h"
6 
7 namespace Graph {
8 
15 template <class Node,class Edge>
16 class DirectedGraph : public Graph<Node,Edge>
17 {
18 public:
19  typedef Graph<Node,Edge> P;
22  void DFS(CallbackBase<Node>& f) { P::DFS(f,ForwardIterator()); }
23  void DFSReverse(CallbackBase<Node>& f) { P::DFS(f,ReverseIterator()); }
24  void SimpleDFS(CallbackBase<Node>& f) { P::SimpleDFS(f,ForwardIterator()); }
25  void SimpleBFS(CallbackBase<Node>& f) { P::SimpleBFS(f,ForwardIterator()); }
26  void GuidedDFS(CallbackBase<Node>& f) { P::GuidedDFS(f,ForwardIterator()); }
27  void GuidedBFS(CallbackBase<Node>& f) { P::GuidedBFS(f,ForwardIterator()); }
28 
29  bool HasDescendent(int n,int d);
30  bool HasAncestor(int n,int a);
31  std::list<int> TopologicalSort();
32  bool HasCycle();
33 };
34 
35 template <class Node,class Edge>
36 bool DirectedGraph<Node,Edge>::HasDescendent(const int n,const int d)
37 {
39  FindCallback<int> findNode(d);
40  ForwardIterator it;
41  return P::_SimpleDFS(n,findNode,it);
42 }
43 
44 template <class Node,class Edge>
45 bool DirectedGraph<Node,Edge>::HasAncestor(const int n,const int a)
46 {
47  return HasDescendent(a,n);
48 }
49 
50 template <class Node,class Edge>
52 {
54  DFS(f);
55  return f.list;
56 }
57 
58 template <class Node,class Edge>
60 {
62  DFS(f);
63  return f.hasCycle;
64 }
65 
66 
67 } //namespace Graph
68 
69 #endif
Definition: Edge.h:28
A template base class for a graph traversal.
Definition: Callback.h:21
Compute if the graph has a cycle.
Definition: Callback.h:105
A specialization of a Graph to be directed graph.
Definition: DirectedGraph.h:16
Namespace for all classes and functions in the Graph subdirectory.
Definition: ApproximateShortestPaths.h:7
Perform topological sort, when used with DFS.
Definition: Callback.h:115
Find a particular node.
Definition: Callback.h:53