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