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