#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE animation_run(SCM k)
{
        Graph *gr;
        if (!INTEGERP(k))
                Err("animation-run: bad arg 1, <integer> expected", k);
	int i = INTEGER(k);
	animationRun(i);
	return UNDEFINED;
}

static PRIMITIVE animation_done(SCM k)
{
        Graph *gr;
        if (!INTEGERP(k))
                Err("animation-done: bad arg 1, <integer> expected", k);
	int i = INTEGER(k);
	animationDone(i);
	return UNDEFINED;
}

static PRIMITIVE animation_continue(SCM k)
{
        Graph *gr;
        if (!INTEGERP(k))
                Err("animation-continue: bad arg 1, <integer> expected", k);
	int i = INTEGER(k);
	animationContinue(i);
	return UNDEFINED;
}

static PRIMITIVE animation_step(SCM k)
{
        Graph *gr;
        if (!INTEGERP(k))
                Err("animation-step: bad arg 1, <integer> expected", k);
	int i = INTEGER(k);
	animationStep(i);
	return UNDEFINED;
}

static PRIMITIVE animation_backup(SCM k)
{
        Graph *gr;
        if (!INTEGERP(k))
                Err("animation-backup: bad arg 1, <integer> expected", k);
	int i = INTEGER(k);
	animationBackup(i);
	return UNDEFINED;
}

static PRIMITIVE animation_break(SCM k)
{
        Graph *gr;
        if (!INTEGERP(k))
                Err("animation-break: bad arg 1, <integer> expected", k);
	int i = INTEGER(k);
	animationBreak(i);
	return UNDEFINED;
}

static PRIMITIVE animation_clearbreaks(SCM k)
{
        Graph *gr;
        if (!INTEGERP(k))
                Err("animation-clear-breaks: bad arg 1, <integer> expected", k);
	int i = INTEGER(k);
	animationClearBreaks(i);
	return UNDEFINED;
}

void gen_Animation_Wrapper()
{
        STk_add_new_cpp_primitive("animation-run", tc_subr_1,
                               (PRIMITIVE (*)(...))animation_run);
        STk_add_new_cpp_primitive("animation-done", tc_subr_1,
                               (PRIMITIVE (*)(...))animation_done);
        STk_add_new_cpp_primitive("animation-continue", tc_subr_1,
                               (PRIMITIVE (*)(...))animation_continue);
        STk_add_new_cpp_primitive("animation-step", tc_subr_1,
                               (PRIMITIVE (*)(...))animation_step);
        STk_add_new_cpp_primitive("animation-backup", tc_subr_1,
                               (PRIMITIVE (*)(...))animation_backup);
        STk_add_new_cpp_primitive("animation-clear-breaks", tc_subr_1,
                               (PRIMITIVE (*)(...))animation_clearbreaks);
        STk_add_new_cpp_primitive("animation-break", tc_subr_1,
                               (PRIMITIVE (*)(...))animation_break);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Set.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE ArticulationPointsCmd(SCM g)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("articulation-points: bad arg 1, graph expected", g);
        Set<Vertex*> s = ArticulationPoints(gr,0);
        Set<Vertex*>* sp = new Set<Vertex*>(s);

        return STk_make_CXXwrapper(Wrapper<Set<Vertex*> >::type,
                Wrapper<Set<Vertex*> >::name, (void *) sp, LINK_DYNAMIC);
}

static PRIMITIVE ArticulationPointsAnimCmd(SCM g, SCM view)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("articulation-points: bad arg 1, graph expected", g);
	char *name = getViewName(view);
	int k = initAnimation();
	animationWindows(k, name);
        Set<Vertex*> s = ArticulationPoints(gr,name);
        Set<Vertex*>* sp = new Set<Vertex*>(s);

        return STk_make_CXXwrapper(Wrapper<Set<Vertex*> >::type,
                Wrapper<Set<Vertex*> >::name, (void *) sp, LINK_DYNAMIC);
}

void gen_ArticulationPoints_Wrapper()
{
        STk_add_new_cpp_primitive("articulation-points", tc_subr_1,
                               (PRIMITIVE (*)(...))ArticulationPointsCmd);
        STk_add_new_cpp_primitive("articulation-points*", tc_subr_2,
                               (PRIMITIVE (*)(...))ArticulationPointsAnimCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Sequence.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE BiconnectedComponentsCmd(SCM g)
{
    Graph *gr;
    if (!(gr = getGraph(g)))
	Err("biconnected-components: bad arg 1, graph expected", g);
    Set< Set<Edge*> > s = BiconnectedComponents(gr);
    Set< Set<Edge*> >* sp = new Set< Set<Edge*> >(s);
    
    return STk_make_CXXwrapper(Wrapper<Set< Set<Edge*> > >::type,
			       Wrapper<Set< Set<Edge*> > >::name, 
			       (void *) sp, LINK_DYNAMIC);
}

static PRIMITIVE BiconnectedComponentsAnimCmd(SCM g, SCM view) 
{
    Graph *gr;
    
    if (!(gr = getGraph(g)))
	Err("biconnected-components: bad arg 1, graph expected", g);
    char *name = getViewName(view);
    int k = initAnimation();
    animationWindows(k, name);
    Set< Set<Edge*> > s = BiconnectedComponents(gr,name);
    Set< Set<Edge*> >* sp = new Set< Set<Edge*> >(s);
    return STk_make_CXXwrapper(Wrapper<Set< Set<Edge*> > >::type,
			       Wrapper<Set< Set<Edge*> > >::name, 
			       (void *) sp, LINK_DYNAMIC);
}

void gen_BiconnectedComponents_Wrapper()
{
    STk_add_new_cpp_primitive("bcc", tc_subr_1,
		 (PRIMITIVE (*)(...))BiconnectedComponentsCmd);
    STk_add_new_cpp_primitive("bcc*", tc_subr_2,
		 (PRIMITIVE (*)(...))BiconnectedComponentsAnimCmd);
    STk_add_new_cpp_primitive("biconnected-components", tc_subr_1,
		 (PRIMITIVE (*)(...))BiconnectedComponentsCmd);
    STk_add_new_cpp_primitive("biconnected-components*", tc_subr_2,
		 (PRIMITIVE (*)(...))BiconnectedComponentsAnimCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Sequence.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>
#include<LINK/graph/Vertex.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE BreadthFirstSearchAnimCmd(SCM parms, int len)
{
    SCM graph, view=UNBOUND, vertex=UNBOUND;
    Graph *gr;
    char *name;
    Vertex *v;
    int& v_type = Wrapper<Vertex*>::type;

    switch (len) {
	case 0: return UNDEFINED;
		break;
	case 1: graph = CAR(parms);
		break;
	case 2: graph = CAR(parms);
		view = CAR(CDR(parms));
		break;
	case 3: graph = CAR(parms);
		view = CAR(CDR(parms));
		vertex = CAR(CDR(CDR(parms)));
		break;
    }
    if (!(gr = getGraph(graph)))
	Err("bfs: bad arg 1, graph expected", graph);
    if ((len > 1) && (!(name = getViewName(view))))
	Err("bfs: bad arg 2, graph view expected", view);
    if ((len > 2) && (!CXX_TYPEP(vertex, v_type)))
	Err("bfs: bad arg 3, <vertex*> expected", vertex);
    Sequence<Vertex*> s; 
    Sequence<Vertex*>* sp;
    if (len > 1) {
    	int k = initAnimation();
    	animationWindows(k, name);
	if (len > 2) {
		v = *(Vertex**) CLASSDATA(vertex);
    		s = BreadthFirstSearch(gr,v,name);
	} else
    		s = BreadthFirstSearch(gr,0,name);
    } else 
    	s = BreadthFirstSearch(gr);
    sp = new Sequence<Vertex*>(s);
    return STk_make_CXXwrapper(Wrapper<Sequence<Vertex*> >::type,
			       Wrapper<Sequence<Vertex*> >::name, (void *) sp, LINK_DYNAMIC);
}

void gen_BreadthFirstSearch_Wrapper()
{
        STk_add_new_cpp_primitive("bfs", tc_lsubr,
				 (PRIMITIVE (*)(...))BreadthFirstSearchAnimCmd);
        STk_add_new_cpp_primitive("breadth-first-search", tc_lsubr,
				 (PRIMITIVE (*)(...))BreadthFirstSearchAnimCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Set.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE BridgesCmd(SCM g)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("bridges: bad arg 1, graph expected", g);
        Set<Edge*> s = Bridges(gr,0);
        Set<Edge*>* sp = new Set<Edge*>(s);

        return STk_make_CXXwrapper(Wrapper<Set<Edge*> >::type,
                Wrapper<Set<Edge*> >::name, (void *) sp, LINK_DYNAMIC);
}

static PRIMITIVE BridgesAnimCmd(SCM g, SCM view)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("bridges: bad arg 1, graph expected", g);
	char *name = getViewName(view);
	int k = initAnimation();
	animationWindows(k, name);
        Set<Edge*> s = Bridges(gr,name);
        Set<Edge*>* sp = new Set<Edge*>(s);

        return STk_make_CXXwrapper(Wrapper<Set<Edge*> >::type,
                Wrapper<Set<Edge*> >::name, (void *) sp, LINK_DYNAMIC);
}

void gen_Bridges_Wrapper()
{
        STk_add_new_cpp_primitive("bridges", tc_subr_1,
                               (PRIMITIVE (*)(...))BridgesCmd);
        STk_add_new_cpp_primitive("bridges*", tc_subr_2,
                               (PRIMITIVE (*)(...))BridgesAnimCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Sequence.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE DepthFirstSearchCmd(SCM g)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("dfs: bad arg 1, graph expected", g);
        Sequence<Vertex*> s = DepthFirstSearch(gr);
        Sequence<Vertex*>* sp = new Sequence<Vertex*>(s);

        return STk_make_CXXwrapper(Wrapper<Sequence<Vertex*> >::type,
                Wrapper<Sequence<Vertex*> >::name, (void *) sp, LINK_DYNAMIC);
}

static PRIMITIVE DepthFirstSearchAnimCmd(SCM g, SCM view)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("dfs: bad arg 1, graph expected", g);
	char *name = getViewName(view);
	int k = initAnimation();
	animationWindows(k, name);
        Sequence<Vertex*> s = DepthFirstSearch(gr,0,name);
        Sequence<Vertex*>* sp = new Sequence<Vertex*>(s);

        return STk_make_CXXwrapper(Wrapper<Sequence<Vertex*> >::type,
                Wrapper<Sequence<Vertex*> >::name, (void *) sp, LINK_DYNAMIC);
}

typedef Vertex* VertexPtr;
extern int rev_compare_vertices_by_finishtime(const VertexPtr&, 
					      const VertexPtr&);

static PRIMITIVE DepthFirstSearchAnimCmd2(SCM g, SCM view)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("dfs: bad arg 1, graph expected", g);
	char *name = getViewName(view);
	int k = initAnimation();
	animationWindows(k, name);
        Sequence<Vertex*> s = 
	       DepthFirstSearch(gr,rev_compare_vertices_by_finishtime,name);
        Sequence<Vertex*>* sp = new Sequence<Vertex*>(s);

        return STk_make_CXXwrapper(Wrapper<Sequence<Vertex*> >::type,
                Wrapper<Sequence<Vertex*> >::name, (void *) sp, LINK_DYNAMIC);
}

void gen_DepthFirstSearch_Wrapper()
{
        STk_add_new_cpp_primitive("dfs", tc_subr_1,
                               (PRIMITIVE (*)(...))DepthFirstSearchCmd);
        STk_add_new_cpp_primitive("dfs*", tc_subr_2,
                               (PRIMITIVE (*)(...))DepthFirstSearchAnimCmd);
        STk_add_new_cpp_primitive("dfs-decreasing-finish-time*", tc_subr_2,
                               (PRIMITIVE (*)(...))DepthFirstSearchAnimCmd2);
        STk_add_new_cpp_primitive("depth-first-search", tc_subr_1,
                               (PRIMITIVE (*)(...))DepthFirstSearchCmd);
        STk_add_new_cpp_primitive("depth-first-search*", tc_subr_2,
                               (PRIMITIVE (*)(...))DepthFirstSearchAnimCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Sequence.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>
#include<LINK/graph/Vertex.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE DijkstraAnimCmd(SCM parms, int len)
{
    SCM graph, view=UNBOUND, vertex=UNBOUND;
    Graph *gr;
    char *name;
    Vertex *v;
    int& v_type = Wrapper<Vertex*>::type;

    switch (len) {
	case 0: return UNDEFINED;
		break;
	case 1: graph = CAR(parms);
		break;
	case 2: graph = CAR(parms);
		view = CAR(CDR(parms));
		break;
	case 3: graph = CAR(parms);
		view = CAR(CDR(parms));
		vertex = CAR(CDR(CDR(parms)));
		break;
    }
    if (!(gr = getGraph(graph)))
	Err("dijkstra: bad arg 1, graph expected", graph);
    if ((len > 1) && (!(name = getViewName(view))))
	Err("dijkstra: bad arg 2, graph view expected", view);
    if ((len > 2) && (!CXX_TYPEP(vertex, v_type)))
	Err("dijkstra: bad arg 3, <vertex*> expected", vertex);
    Sequence<double> s; 
    if (len > 1) {
    	int k = initAnimation();
    	animationWindows(k, name);
	if (len > 2) {
		v = *(Vertex**) CLASSDATA(vertex);
    		s = Dijkstra(gr,name, v);
	} else
    		s = Dijkstra(gr,name, v);
    } else 
    	s = Dijkstra(gr);
    return UNDEFINED;
}

void gen_Dijkstra_Wrapper()
{
        STk_add_new_cpp_primitive("dijkstra", tc_lsubr,
				 (PRIMITIVE (*)(...))DijkstraAnimCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Sequence.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE FleuryCmd(SCM g)
{
    Graph *gr;
    if (!(gr = getGraph(g)))
	Err("fleury: bad arg 1, graph expected", g);
    Sequence<Edge*> s = Fleury(gr);
    Sequence<Edge*>* sp = new Sequence<Edge*>(s);
    
    return STk_make_CXXwrapper(Wrapper<Sequence<Edge*> >::type,
			       Wrapper<Sequence<Edge*> >::name, 
			       (void *) sp, LINK_DYNAMIC);
}

static PRIMITIVE FleuryAnimCmd(SCM g, SCM view) 
{
    Graph *gr;
    
    if (!(gr = getGraph(g)))
	Err("fleury: bad arg 1, graph expected", g);
    char *name = getViewName(view);
    int k = initAnimation();
    animationWindows(k, name);
    Sequence<Edge*> s = Fleury(gr, name);
    Sequence<Edge*>* sp = new Sequence<Edge*>(s);
    return STk_make_CXXwrapper(Wrapper<Sequence<Edge*> >::type,
			       Wrapper<Sequence<Edge*> >::name, 
			       (void *) sp, LINK_DYNAMIC);
}

void gen_Fleury_Wrapper()
{
    STk_add_new_cpp_primitive("fleury", tc_subr_1,
		 (PRIMITIVE (*)(...))FleuryCmd);
    STk_add_new_cpp_primitive("fleury*", tc_subr_2,
		 (PRIMITIVE (*)(...))FleuryAnimCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Matrix.h>
#include<LINK/graph/Graph.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE FloydWarshallCmd(SCM g)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("floyd-warshall: bad arg 1, graph expected", g);
        Matrix<double> m = FloydWarshall(gr);
        Matrix<double>* mp = new Matrix<double>(m);

        return STk_make_CXXwrapper(Wrapper<Matrix<double> >::type,
                Wrapper<Matrix<double> >::name, (void *) mp, LINK_DYNAMIC);
}

void gen_FloydWarshall_Wrapper()
{
        STk_add_new_cpp_primitive("floyd-warshall", tc_subr_1,
                               (PRIMITIVE (*)(...))FloydWarshallCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Set.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE HamiltonianCyclesCmd(SCM g)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("directed-triangles: bad arg 1, graph expected", g);
        HamiltonianCycles(gr,0);
        return UNDEFINED;
}

void gen_HamiltonianCycles_Wrapper()
{
        STk_add_new_cpp_primitive("hamiltonian-cycles", tc_subr_1,
                               (PRIMITIVE (*)(...))HamiltonianCyclesCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/graph/Graph.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE GirthCmd(SCM g)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("bridges: bad arg 1, graph expected", g);
        int i = Girth(gr);

        return STk_makeinteger(i);
}

void gen_Girth_Wrapper()
{
        STk_add_new_cpp_primitive("girth", tc_subr_1,
                               (PRIMITIVE (*)(...))GirthCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/HyperGraph.h>
#include<LINK/graph/Edge.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE GoldbergTarjanCmd(SCM parms, int len)
{
	if (len < 1)
                Err("goldberg-tarjan <graph*> <a|b|c>? <A|B>? <Y|?>?",NIL);

        Graph *gr;
	SCM   stk_graph = CAR(parms); // CAR,CDR: see stk.h
	SCM   args = CDR(parms);

        if (!(gr = getGraph(stk_graph))) // getGraph: stkUtils.cpp
                Err("goldberg-tarjan: bad arg 1, graph expected", stk_graph);
	char vertexselection = LIFO;
	char relabelchoice   = GLOBAL;
	char labelingchoice  = GLOBAL;
	if (!NULLP(args)) {
		if (!INTEGERP(CAR(args)))
                	Err("goldberg-tarjan: bad arg 2, string or symbol",
							CAR(args));
		vertexselection = INTEGER(CAR(args));
		args = CDR(args);
	}
	if (!NULLP(args)) {
		if (!INTEGERP(CAR(args)))
                	Err("goldberg-tarjan: bad arg 3, string or symbol",
							CAR(args));
		relabelchoice = INTEGER(CAR(args));
		args = CDR(args);
	}
	if (!NULLP(args)) {
		if (!INTEGERP(CAR(args)))
                	Err("goldberg-tarjan: bad arg 4, string or symbol",
							CAR(args));
		labelingchoice = INTEGER(CAR(args));
		args = CDR(args);
	}
        int maxflow = GoldbergTarjan(gr,vertexselection,relabelchoice,
				  labelingchoice);
	return STk_makeinteger(maxflow);
}

void gen_GoldbergTarjan_Wrapper()
{
        STk_add_new_cpp_primitive("goldberg-tarjan", tc_lsubr,
                             (PRIMITIVE (*)(...))GoldbergTarjanCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/algorithm/Partitioning.h>
#include<LINK/basic/Sequence.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>
#include<LINK/graph/Vertex.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE KLFMCmd(SCM parms, int len)
{
    SCM graph, view=UNBOUND, problem_type=UNBOUND, iterations=UNBOUND;
    Graph *gr;
    char *name=0;
    Vertex *v;
    int& v_type = Wrapper<Vertex*>::type;

    if (len == 0)
	return UNDEFINED;
    if (len >= 1)
	graph = CAR(parms);
    if (len >= 2)
	problem_type = CAR(CDR(parms));
    if (len >= 3)
	iterations = CAR(CDR(CDR(parms)));

    if (!(gr = getGraph(graph)))
	Err("klfm: bad arg 1, graph expected", graph);
    if ((len > 1) && (!INTEGERP(problem_type)))
	Err("klfm: bad arg 2, integer expected (problem type)", problem_type);
    if ((len > 2) && (!INTEGERP(iterations)))
	Err("klfm: bad arg 3, integer expected (iterations)", iterations);
    int cuts=0;
    int iter = 10;
    int prob_type = MAX_CUT;

    if (INTEGERP(problem_type))
	prob_type = INTEGER(problem_type);
    if (INTEGERP(iterations))
	iter = INTEGER(iterations);

    if (len > 2) {
 	cuts = KLFM(gr,prob_type, iter);
    } else if (len > 1) {
    	cuts = KLFM(gr,prob_type);
    } else 
    	cuts = KLFM(gr);
    return STk_makeinteger(cuts);
}

void gen_KLFM_Wrapper()
{
        STk_add_new_cpp_primitive("klfm", tc_lsubr,
				 (PRIMITIVE (*)(...))KLFMCmd);
        STk_add_new_cpp_primitive("breadth-first-search", tc_lsubr,
				 (PRIMITIVE (*)(...))KLFMCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Set.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE IntToHexString(SCM i)
{
	if (!INTEGERP(i))
		err("int-to-hex-string: integer expected", i);
	int v = INTEGER(i);
        char str[80];
        sprintf(str, "%x", v);
	return STk_makestring(str);
}

static PRIMITIVE KruskalCmd(SCM g)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("kruskal: bad arg 1, graph expected", g);
        Set<Edge*> s = Kruskal(gr,0);
        Set<Edge*>* sp = new Set<Edge*>(s);

        return STk_make_CXXwrapper(Wrapper<Set<Edge*> >::type,
                Wrapper<Set<Edge*> >::name, (void *) sp, LINK_DYNAMIC);
}

static PRIMITIVE KruskalAnimCmd(SCM g, SCM view=0)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("kruskal: bad arg 1, graph expected", g);
	char *name = getViewName(view);
	if (name) {
		int k = initAnimation();
		animationWindows(k, name);
	}
        Set<Edge*> s = Kruskal(gr,name);
        Set<Edge*>* sp = new Set<Edge*>(s);

        return STk_make_CXXwrapper(Wrapper<Set<Edge*> >::type,
                Wrapper<Set<Edge*> >::name, (void *) sp, LINK_DYNAMIC);
}

void gen_Kruskal_Wrapper()
{
//        STk_add_new_cpp_primitive("kruskal", tc_subr_1,
//                               (PRIMITIVE (*)(...))KruskalCmd);
        STk_add_new_cpp_primitive("kruskal", tc_subr_1_or_2,
                               (PRIMITIVE (*)(...))KruskalAnimCmd);
        STk_add_new_cpp_primitive("int-to-hex-string", tc_subr_1,
                                    (PRIMITIVE (*)(...))IntToHexString);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/basic/Set.h>
#include<LINK/graph/Vertex.h>
#include<LINK/graph/Edge.h>
#include<LINK/graph/Graph.h>
#include<LINK/algorithm/Algorithms.h>

PRIMITIVE MinWeightPerfectMatchingWrapper(SCM g)
{
	Graph *gr;
	if (!(gr = getGraph(g)))   // see src/stkWrapper/stkUtils.cc
		Err("min-weight-perfect-matching: expected <graph*> ", g);

	Set<Edge*> result = MinWeightPerfectMatching(gr);
	Set<Edge*>* new_copy = new Set<Edge*>(result);

	return STk_make_CXXwrapper(
		Wrapper<Set<Edge*> >::type,	// stkWrapper/Wrapper.h
		Wrapper<Set<Edge*> >::name,
		(void *) new_copy,	
		LINK_DYNAMIC);		// tells STk's garbage collector
					// to delete new_copy when the user
					// is finished with it
}

//
// This is what must go into userinit.cc
//
void gen_MinWeightPerfectMatching_Wrapper()
{
        STk_add_new_cpp_primitive("min-weight-perfect-matching", // cmd name
		  tc_subr_1,		// # parms
                  (PRIMITIVE (*)(...))MinWeightPerfectMatchingWrapper);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Sequence.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>
#include<LINK/graph/Vertex.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE PrimAnimCmd(SCM parms, int len)
{
    SCM graph, view=UNBOUND, vertex=UNBOUND;
    Graph *gr;
    char *name;
    Vertex *v;
    int& v_type = Wrapper<Vertex*>::type;

    switch (len) {
	case 0: return UNDEFINED;
		break;
	case 1: graph = CAR(parms);
		break;
	case 2: graph = CAR(parms);
		view = CAR(CDR(parms));
		break;
	case 3: graph = CAR(parms);
		view = CAR(CDR(parms));
		vertex = CAR(CDR(CDR(parms)));
		break;
    }
    if (!(gr = getGraph(graph)))
	Err("prim: bad arg 1, graph expected", graph);
    if ((len > 1) && (!(name = getViewName(view))))
	Err("prim: bad arg 2, graph view expected", view);
    if ((len > 2) && (!CXX_TYPEP(vertex, v_type)))
	Err("prim: bad arg 3, <vertex*> expected", vertex);
    Set<Edge*> s; 
    Set<Edge*>* sp;
    if (len > 1) {
    	int k = initAnimation();
    	animationWindows(k, name);
	if (len > 2) {
		v = *(Vertex**) CLASSDATA(vertex);
    		s = Prim(gr,name, v);
	} else
    		s = Prim(gr,name, 0);
    } else 
    	s = Prim(gr);
    sp = new Set<Edge*>(s);
    return STk_make_CXXwrapper(Wrapper<Set<Edge*> >::type,
			       Wrapper<Set<Edge*> >::name, (void *) sp, LINK_DYNAMIC);
}

void gen_Prim_Wrapper()
{
        STk_add_new_cpp_primitive("prim", tc_lsubr,
				 (PRIMITIVE (*)(...))PrimAnimCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/graph/Graph.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE RadiusCmd(SCM g)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("bridges: bad arg 1, graph expected", g);
        double d = Radius(gr);

        return STk_makenumber(d);
}

void gen_Radius_Wrapper()
{
        STk_add_new_cpp_primitive("radius", tc_subr_1,
                               (PRIMITIVE (*)(...))RadiusCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE RevealEdgesByRankCmd(SCM g, SCM view)
{
	Graph *gr;
        if (!(gr = getGraph(g)))
                Err("reveal-edges-by-rank: bad arg 1, graph expected", g);
	char *name = getViewName(view);
	int k = initAnimation();
	animationWindows(k, name);
        RevealEdgesByRank(gr,name);

        return UNDEFINED;
}

void gen_RevealEdgesByRank_Wrapper()
{
        STk_add_new_cpp_primitive("reveal-edges-by-rank", tc_subr_2,
                               (PRIMITIVE (*)(...))RevealEdgesByRankCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Set.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE TrianglesCmd(SCM g)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("directed-triangles: bad arg 1, graph expected", g);
        Triangles(gr,0);
        return UNDEFINED;
}

void gen_Triangles_Wrapper()
{
        STk_add_new_cpp_primitive("find-triangles", tc_subr_1,
                               (PRIMITIVE (*)(...))TrianglesCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Sequence.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE TopologicalSortCmd(SCM g)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("topsort: bad arg 1, graph expected", g);
        Sequence<Vertex*> s = TopologicalSort(gr);
        Sequence<Vertex*>* sp = new Sequence<Vertex*>(s);

        return STk_make_CXXwrapper(Wrapper<Sequence<Vertex*> >::type,
                Wrapper<Sequence<Vertex*> >::name, (void *) sp, LINK_DYNAMIC);
}

static PRIMITIVE TopologicalSortAnimCmd(SCM g, SCM view) {
    Graph *gr;
    
    if (!(gr = getGraph(g)))
	Err("topsort: bad arg 1, graph expected", g);
    char *name = getViewName(view);
    int k = initAnimation();
    animationWindows(k, name);
    Sequence<Vertex*> s = TopologicalSort(gr,name);
    Sequence<Vertex*>* sp = new Sequence<Vertex*>(s);
    return STk_make_CXXwrapper(Wrapper<Sequence<Vertex*> >::type,
			       Wrapper<Sequence<Vertex*> >::name, (void *) sp, LINK_DYNAMIC);
}

void gen_TopologicalSort_Wrapper()
{
    STk_add_new_cpp_primitive("topsort", tc_subr_1,
			      (PRIMITIVE (*)(...))TopologicalSortCmd);
    STk_add_new_cpp_primitive("topsort*", tc_subr_2,
			      (PRIMITIVE (*)(...))TopologicalSortAnimCmd);
    STk_add_new_cpp_primitive("topological-sort", tc_subr_1,
			      (PRIMITIVE (*)(...))TopologicalSortCmd);
    STk_add_new_cpp_primitive("topological-sort*", tc_subr_2,
			      (PRIMITIVE (*)(...))TopologicalSortAnimCmd);
}
#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Sequence.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE StronglyConnectedComponentsCmd(SCM g)
{
    Graph *gr;
    if (!(gr = getGraph(g)))
	Err("strongly-connected-components: bad arg 1, graph expected", g);
    Set< Set<Vertex*> > s = StronglyConnectedComponents(gr);
    Set< Set<Vertex*> >* sp = new Set< Set<Vertex*> >(s);
    
    return STk_make_CXXwrapper(Wrapper<Set< Set<Vertex*> > >::type,
			       Wrapper<Set< Set<Vertex*> > >::name, 
			       (void *) sp, LINK_DYNAMIC);
}

static PRIMITIVE StronglyConnectedComponentsAnimCmd(SCM g, SCM view) 
{
    Graph *gr;
    
    if (!(gr = getGraph(g)))
	Err("strongly-connected-components: bad arg 1, graph expected", g);
    char *name = getViewName(view);
    int k = initAnimation();
    animationWindows(k, name);
    Set< Set<Vertex*> > s = StronglyConnectedComponents(gr,name);
    Set< Set<Vertex*> >* sp = new Set< Set<Vertex*> >(s);
    return STk_make_CXXwrapper(Wrapper<Set< Set<Vertex*> > >::type,
			       Wrapper<Set< Set<Vertex*> > >::name, 
			       (void *) sp, LINK_DYNAMIC);
}

void gen_StronglyConnectedComponents_Wrapper()
{
    STk_add_new_cpp_primitive("scc", tc_subr_1,
		 (PRIMITIVE (*)(...))StronglyConnectedComponentsCmd);
    STk_add_new_cpp_primitive("scc*", tc_subr_2,
		 (PRIMITIVE (*)(...))StronglyConnectedComponentsAnimCmd);
    STk_add_new_cpp_primitive("strongly-connected-components", tc_subr_1,
		 (PRIMITIVE (*)(...))StronglyConnectedComponentsCmd);
    STk_add_new_cpp_primitive("strongly-connected-components*", tc_subr_2,
		 (PRIMITIVE (*)(...))StronglyConnectedComponentsAnimCmd);
}
