kpeter@696: /* -*- mode: C++; indent-tabs-mode: nil; -*- kpeter@696: * kpeter@696: * This file is a part of LEMON, a generic C++ optimization library. kpeter@696: * kpeter@696: * Copyright (C) 2003-2009 kpeter@696: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@696: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@696: * kpeter@696: * Permission to use, modify and distribute this software is granted kpeter@696: * provided that this copyright notice appears in all copies. For kpeter@696: * precise terms see the accompanying LICENSE file. kpeter@696: * kpeter@696: * This software is provided "AS IS" with no warranty of any kind, kpeter@696: * express or implied, and with no claim as to its suitability for any kpeter@696: * purpose. kpeter@696: * kpeter@696: */ kpeter@696: kpeter@696: #include kpeter@696: #include kpeter@696: #include kpeter@696: kpeter@696: #include "test_tools.h" kpeter@696: kpeter@696: using namespace lemon; kpeter@696: kpeter@696: kpeter@696: int main() kpeter@696: { kpeter@696: typedef ListDigraph Digraph; kpeter@696: typedef Undirector Graph; kpeter@696: kpeter@696: { kpeter@696: Digraph d; kpeter@696: Digraph::NodeMap order(d); kpeter@696: Graph g(d); kpeter@696: kpeter@696: check(stronglyConnected(d), "The empty digraph is strongly connected"); kpeter@696: check(countStronglyConnectedComponents(d) == 0, kpeter@696: "The empty digraph has 0 strongly connected component"); kpeter@696: check(connected(g), "The empty graph is connected"); kpeter@696: check(countConnectedComponents(g) == 0, kpeter@696: "The empty graph has 0 connected component"); kpeter@696: kpeter@696: check(biNodeConnected(g), "The empty graph is bi-node-connected"); kpeter@696: check(countBiNodeConnectedComponents(g) == 0, kpeter@696: "The empty graph has 0 bi-node-connected component"); kpeter@696: check(biEdgeConnected(g), "The empty graph is bi-edge-connected"); kpeter@696: check(countBiEdgeConnectedComponents(g) == 0, kpeter@696: "The empty graph has 0 bi-edge-connected component"); kpeter@696: kpeter@696: check(dag(d), "The empty digraph is DAG."); kpeter@696: check(checkedTopologicalSort(d, order), "The empty digraph is DAG."); kpeter@696: check(loopFree(d), "The empty digraph is loop-free."); kpeter@696: check(parallelFree(d), "The empty digraph is parallel-free."); kpeter@696: check(simpleGraph(d), "The empty digraph is simple."); kpeter@696: kpeter@696: check(acyclic(g), "The empty graph is acyclic."); kpeter@696: check(tree(g), "The empty graph is tree."); kpeter@696: check(bipartite(g), "The empty graph is bipartite."); kpeter@696: check(loopFree(g), "The empty graph is loop-free."); kpeter@696: check(parallelFree(g), "The empty graph is parallel-free."); kpeter@696: check(simpleGraph(g), "The empty graph is simple."); kpeter@696: } kpeter@696: kpeter@696: { kpeter@696: Digraph d; kpeter@696: Digraph::NodeMap order(d); kpeter@696: Graph g(d); kpeter@696: Digraph::Node n = d.addNode(); kpeter@696: kpeter@696: check(stronglyConnected(d), "This digraph is strongly connected"); kpeter@696: check(countStronglyConnectedComponents(d) == 1, kpeter@696: "This digraph has 1 strongly connected component"); kpeter@696: check(connected(g), "This graph is connected"); kpeter@696: check(countConnectedComponents(g) == 1, kpeter@696: "This graph has 1 connected component"); kpeter@696: kpeter@696: check(biNodeConnected(g), "This graph is bi-node-connected"); kpeter@696: check(countBiNodeConnectedComponents(g) == 0, kpeter@696: "This graph has 0 bi-node-connected component"); kpeter@696: check(biEdgeConnected(g), "This graph is bi-edge-connected"); kpeter@696: check(countBiEdgeConnectedComponents(g) == 1, kpeter@696: "This graph has 1 bi-edge-connected component"); kpeter@696: kpeter@696: check(dag(d), "This digraph is DAG."); kpeter@696: check(checkedTopologicalSort(d, order), "This digraph is DAG."); kpeter@696: check(loopFree(d), "This digraph is loop-free."); kpeter@696: check(parallelFree(d), "This digraph is parallel-free."); kpeter@696: check(simpleGraph(d), "This digraph is simple."); kpeter@696: kpeter@696: check(acyclic(g), "This graph is acyclic."); kpeter@696: check(tree(g), "This graph is tree."); kpeter@696: check(bipartite(g), "This graph is bipartite."); kpeter@696: check(loopFree(g), "This graph is loop-free."); kpeter@696: check(parallelFree(g), "This graph is parallel-free."); kpeter@696: check(simpleGraph(g), "This graph is simple."); kpeter@696: } kpeter@696: kpeter@696: { kpeter@696: Digraph d; kpeter@696: Digraph::NodeMap order(d); kpeter@696: Graph g(d); kpeter@696: kpeter@696: Digraph::Node n1 = d.addNode(); kpeter@696: Digraph::Node n2 = d.addNode(); kpeter@696: Digraph::Node n3 = d.addNode(); kpeter@696: Digraph::Node n4 = d.addNode(); kpeter@696: Digraph::Node n5 = d.addNode(); kpeter@696: Digraph::Node n6 = d.addNode(); kpeter@696: kpeter@696: d.addArc(n1, n3); kpeter@696: d.addArc(n3, n2); kpeter@696: d.addArc(n2, n1); kpeter@696: d.addArc(n4, n2); kpeter@696: d.addArc(n4, n3); kpeter@696: d.addArc(n5, n6); kpeter@696: d.addArc(n6, n5); kpeter@696: kpeter@696: check(!stronglyConnected(d), "This digraph is not strongly connected"); kpeter@696: check(countStronglyConnectedComponents(d) == 3, kpeter@696: "This digraph has 3 strongly connected components"); kpeter@696: check(!connected(g), "This graph is not connected"); kpeter@696: check(countConnectedComponents(g) == 2, kpeter@696: "This graph has 2 connected components"); kpeter@696: kpeter@696: check(!dag(d), "This digraph is not DAG."); kpeter@696: check(!checkedTopologicalSort(d, order), "This digraph is not DAG."); kpeter@696: check(loopFree(d), "This digraph is loop-free."); kpeter@696: check(parallelFree(d), "This digraph is parallel-free."); kpeter@696: check(simpleGraph(d), "This digraph is simple."); kpeter@696: kpeter@696: check(!acyclic(g), "This graph is not acyclic."); kpeter@696: check(!tree(g), "This graph is not tree."); kpeter@696: check(!bipartite(g), "This graph is not bipartite."); kpeter@696: check(loopFree(g), "This graph is loop-free."); kpeter@696: check(!parallelFree(g), "This graph is not parallel-free."); kpeter@696: check(!simpleGraph(g), "This graph is not simple."); kpeter@696: kpeter@696: d.addArc(n3, n3); kpeter@696: kpeter@696: check(!loopFree(d), "This digraph is not loop-free."); kpeter@696: check(!loopFree(g), "This graph is not loop-free."); kpeter@696: check(!simpleGraph(d), "This digraph is not simple."); kpeter@696: kpeter@696: d.addArc(n3, n2); kpeter@696: kpeter@696: check(!parallelFree(d), "This digraph is not parallel-free."); kpeter@696: } kpeter@696: kpeter@696: { kpeter@696: Digraph d; kpeter@696: Digraph::ArcMap cutarcs(d, false); kpeter@696: Graph g(d); kpeter@696: kpeter@696: Digraph::Node n1 = d.addNode(); kpeter@696: Digraph::Node n2 = d.addNode(); kpeter@696: Digraph::Node n3 = d.addNode(); kpeter@696: Digraph::Node n4 = d.addNode(); kpeter@696: Digraph::Node n5 = d.addNode(); kpeter@696: Digraph::Node n6 = d.addNode(); kpeter@696: Digraph::Node n7 = d.addNode(); kpeter@696: Digraph::Node n8 = d.addNode(); kpeter@696: kpeter@696: d.addArc(n1, n2); kpeter@696: d.addArc(n5, n1); kpeter@696: d.addArc(n2, n8); kpeter@696: d.addArc(n8, n5); kpeter@696: d.addArc(n6, n4); kpeter@696: d.addArc(n4, n6); kpeter@696: d.addArc(n2, n5); kpeter@696: d.addArc(n1, n8); kpeter@696: d.addArc(n6, n7); kpeter@696: d.addArc(n7, n6); kpeter@696: kpeter@696: check(!stronglyConnected(d), "This digraph is not strongly connected"); kpeter@696: check(countStronglyConnectedComponents(d) == 3, kpeter@696: "This digraph has 3 strongly connected components"); kpeter@696: Digraph::NodeMap scomp1(d); kpeter@696: check(stronglyConnectedComponents(d, scomp1) == 3, kpeter@696: "This digraph has 3 strongly connected components"); kpeter@696: check(scomp1[n1] != scomp1[n3] && scomp1[n1] != scomp1[n4] && kpeter@696: scomp1[n3] != scomp1[n4], "Wrong stronglyConnectedComponents()"); kpeter@696: check(scomp1[n1] == scomp1[n2] && scomp1[n1] == scomp1[n5] && kpeter@696: scomp1[n1] == scomp1[n8], "Wrong stronglyConnectedComponents()"); kpeter@696: check(scomp1[n4] == scomp1[n6] && scomp1[n4] == scomp1[n7], kpeter@696: "Wrong stronglyConnectedComponents()"); kpeter@696: Digraph::ArcMap scut1(d, false); kpeter@696: check(stronglyConnectedCutArcs(d, scut1) == 0, kpeter@696: "This digraph has 0 strongly connected cut arc."); kpeter@696: for (Digraph::ArcIt a(d); a != INVALID; ++a) { kpeter@696: check(!scut1[a], "Wrong stronglyConnectedCutArcs()"); kpeter@696: } kpeter@696: kpeter@696: check(!connected(g), "This graph is not connected"); kpeter@696: check(countConnectedComponents(g) == 3, kpeter@696: "This graph has 3 connected components"); kpeter@696: Graph::NodeMap comp(g); kpeter@696: check(connectedComponents(g, comp) == 3, kpeter@696: "This graph has 3 connected components"); kpeter@696: check(comp[n1] != comp[n3] && comp[n1] != comp[n4] && kpeter@696: comp[n3] != comp[n4], "Wrong connectedComponents()"); kpeter@696: check(comp[n1] == comp[n2] && comp[n1] == comp[n5] && kpeter@696: comp[n1] == comp[n8], "Wrong connectedComponents()"); kpeter@696: check(comp[n4] == comp[n6] && comp[n4] == comp[n7], kpeter@696: "Wrong connectedComponents()"); kpeter@696: kpeter@696: cutarcs[d.addArc(n3, n1)] = true; kpeter@696: cutarcs[d.addArc(n3, n5)] = true; kpeter@696: cutarcs[d.addArc(n3, n8)] = true; kpeter@696: cutarcs[d.addArc(n8, n6)] = true; kpeter@696: cutarcs[d.addArc(n8, n7)] = true; kpeter@696: kpeter@696: check(!stronglyConnected(d), "This digraph is not strongly connected"); kpeter@696: check(countStronglyConnectedComponents(d) == 3, kpeter@696: "This digraph has 3 strongly connected components"); kpeter@696: Digraph::NodeMap scomp2(d); kpeter@696: check(stronglyConnectedComponents(d, scomp2) == 3, kpeter@696: "This digraph has 3 strongly connected components"); kpeter@696: check(scomp2[n3] == 0, "Wrong stronglyConnectedComponents()"); kpeter@696: check(scomp2[n1] == 1 && scomp2[n2] == 1 && scomp2[n5] == 1 && kpeter@696: scomp2[n8] == 1, "Wrong stronglyConnectedComponents()"); kpeter@696: check(scomp2[n4] == 2 && scomp2[n6] == 2 && scomp2[n7] == 2, kpeter@696: "Wrong stronglyConnectedComponents()"); kpeter@696: Digraph::ArcMap scut2(d, false); kpeter@696: check(stronglyConnectedCutArcs(d, scut2) == 5, kpeter@696: "This digraph has 5 strongly connected cut arcs."); kpeter@696: for (Digraph::ArcIt a(d); a != INVALID; ++a) { kpeter@696: check(scut2[a] == cutarcs[a], "Wrong stronglyConnectedCutArcs()"); kpeter@696: } kpeter@696: } kpeter@696: kpeter@696: { kpeter@696: // DAG example for topological sort from the book New Algorithms kpeter@696: // (T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein) kpeter@696: Digraph d; kpeter@696: Digraph::NodeMap order(d); kpeter@696: kpeter@696: Digraph::Node belt = d.addNode(); kpeter@696: Digraph::Node trousers = d.addNode(); kpeter@696: Digraph::Node necktie = d.addNode(); kpeter@696: Digraph::Node coat = d.addNode(); kpeter@696: Digraph::Node socks = d.addNode(); kpeter@696: Digraph::Node shirt = d.addNode(); kpeter@696: Digraph::Node shoe = d.addNode(); kpeter@696: Digraph::Node watch = d.addNode(); kpeter@696: Digraph::Node pants = d.addNode(); kpeter@696: kpeter@696: d.addArc(socks, shoe); kpeter@696: d.addArc(pants, shoe); kpeter@696: d.addArc(pants, trousers); kpeter@696: d.addArc(trousers, shoe); kpeter@696: d.addArc(trousers, belt); kpeter@696: d.addArc(belt, coat); kpeter@696: d.addArc(shirt, belt); kpeter@696: d.addArc(shirt, necktie); kpeter@696: d.addArc(necktie, coat); kpeter@696: kpeter@696: check(dag(d), "This digraph is DAG."); kpeter@696: topologicalSort(d, order); kpeter@696: for (Digraph::ArcIt a(d); a != INVALID; ++a) { kpeter@696: check(order[d.source(a)] < order[d.target(a)], kpeter@696: "Wrong topologicalSort()"); kpeter@696: } kpeter@696: } kpeter@696: kpeter@696: { kpeter@696: ListGraph g; kpeter@696: ListGraph::NodeMap map(g); kpeter@696: kpeter@696: ListGraph::Node n1 = g.addNode(); kpeter@696: ListGraph::Node n2 = g.addNode(); kpeter@696: ListGraph::Node n3 = g.addNode(); kpeter@696: ListGraph::Node n4 = g.addNode(); kpeter@696: ListGraph::Node n5 = g.addNode(); kpeter@696: ListGraph::Node n6 = g.addNode(); kpeter@696: ListGraph::Node n7 = g.addNode(); kpeter@696: kpeter@696: g.addEdge(n1, n3); kpeter@696: g.addEdge(n1, n4); kpeter@696: g.addEdge(n2, n5); kpeter@696: g.addEdge(n3, n6); kpeter@696: g.addEdge(n4, n6); kpeter@696: g.addEdge(n4, n7); kpeter@696: g.addEdge(n5, n7); kpeter@696: kpeter@696: check(bipartite(g), "This graph is bipartite"); kpeter@696: check(bipartitePartitions(g, map), "This graph is bipartite"); kpeter@696: kpeter@696: check(map[n1] == map[n2] && map[n1] == map[n6] && map[n1] == map[n7], kpeter@696: "Wrong bipartitePartitions()"); kpeter@696: check(map[n3] == map[n4] && map[n3] == map[n5], kpeter@696: "Wrong bipartitePartitions()"); kpeter@696: } kpeter@696: kpeter@696: return 0; kpeter@696: }