File: sgip.cpp

package info (click to toggle)
seqan2 2.3.1%2Bdfsg-4~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 222,312 kB
  • sloc: cpp: 252,894; ansic: 86,805; python: 6,534; sh: 985; xml: 570; makefile: 236; awk: 51
file content (280 lines) | stat: -rwxr-xr-x 10,564 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// ===========================================================================
//                 SGIP - Solution of Graph Isomorphism Problem
// ===========================================================================
// Copyright (C) 2012 by Jialu Hu
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your options) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ===========================================================================
// Author: Jialu Hu <Jialu.Hu@fu-berlin.de>
// ===========================================================================
// This application is used to determine whether two given graphs are
// isomorphic or not by using heuristic approach.
// ===========================================================================

#include <time.h>

#include <seqan/basic.h>
#include <seqan/sequence.h>
#include <seqan/graph_types.h>
#include <seqan/arg_parse.h>

#include "sgip.h"
#include "sgip_base.h"
#include "sgip_output.h"

using namespace seqan;

// ==========================================================================
// Forwards
// ==========================================================================

// ==========================================================================
// Tags, Classes, Enums
// ==========================================================================

// --------------------------------------------------------------------------
// Enum FileOption
// --------------------------------------------------------------------------

enum FileOption
{
    ZERO,                  // graph given through parameter
    FIRST,                 // graph created from  orignalFile
    SECOND                 // graph created from  comparisive file
};

// --------------------------------------------------------------------------
// Enum SearchingType
// --------------------------------------------------------------------------

enum SearchingType
{
    HEURISTIC,            // heuristic searching approach
    BRUTEFORTH
};

// --------------------------------------------------------------------------
// Enum SgipOption
// --------------------------------------------------------------------------

// Option for sgip.
struct SgipOption
{
    // I/O options.
    CharString originalFile;        // name of original file(first graph)
    CharString comparFile;         // name of comparisive file(second graph)
    CharString outputFile;         // name of result file
    CharString compareFolder;

    // More options.
    bool autoMetric;               // search Metric dimension if true
    FileOption activeFile;
    SearchingType searchingType;
    CharString algorithm;          // Search strategy for metric dimension,e.g. Greedy, genetic etc.
    unsigned odimension;           // metric dimension of original graph specified by user
    unsigned cdimension;           // metric dimension of comparative graph specified by user
    bool showHelp;
    bool showVersion;
    bool isoCheck;                //to check whether two input graphs are isomorphic
    bool isPrintFile;             //print result if outputFile is specified
    bool isAllatOnce;
    int verbose;

    SgipOption()
    {
        algorithm = "greedy";
        autoMetric = false;
        activeFile = FIRST;
        searchingType = HEURISTIC;
        showHelp = 0;
        showVersion = 0;
        isoCheck = 0;
        isPrintFile = false;
        odimension = 3;
        cdimension = 3;
        verbose = 0;
        isAllatOnce = false;
    }
};

// ==========================================================================
// Functions
// ==========================================================================

// --------------------------------------------------------------------------
// Function _sgip()
// --------------------------------------------------------------------------

// Test sgip with Options.
template <typename TOption>
int _sgip(TOption & options)
{
    typedef Graph<Directed<> > TGraph;
    typedef String<bool>       TMat;
    TMat leastmat1, leastmat2;
    TGraph g1, g2;
    char const * file1, * file2;
    if (!options.isoCheck)
    {
        file1 = toCString(options.originalFile);
        if (!_createGraph(g1, SivaLab(), file1))
            return 1;

        getCanonicalLabel(leastmat1, g1);
        if (options.verbose > 1)
            outputLabel(leastmat1, FFFF());
    }
    else
    {
        file1 = toCString(options.originalFile);
        file2 = toCString(options.comparFile);
        if (!_createGraph(g1, SivaLab(), file1))
            return 1;

        if (!_createGraph(g2, SivaLab(), file2))
            return 1;

        if (checkIsomorphic(g1, g2))
        {
            if (options.verbose > 0)
                std::cout << "They are isomorphic!" << std::endl;
        }
        else
        {
            if (options.verbose > 0)
                std::cout << "They are not isomorphic!" << std::endl;
        }
    }
    return 0;
}

// --------------------------------------------------------------------------
// Function setupParser()
// --------------------------------------------------------------------------

// Set up parser.
template <typename TParser>
void _setupParser(TParser & parser)
{
    setVersion(parser, SEQAN_APP_VERSION " [" SEQAN_REVISION "]");
    setShortDescription(parser, "Solution of Graph Isomorphism Problem");
    addUsageLine(parser, "-o <original graph> [Option]");    
    addSection(parser, "Mandatory Options");
    addOption(parser, ArgParseOption("o", "original", "File containing original graph", ArgParseArgument::INPUT_FILE,"IN"));
    setRequired(parser, "o");
    addSection(parser, "Main Options");
    addOption(parser, ArgParseOption("a", "algorithm", "Algorithm used for searching metric dimension", ArgParseArgument::STRING));
    setDefaultValue(parser, "algorithm", "greedy");
    addOption(parser, ArgParseOption("s", "searching", "Searching algorithm used for detecting resolving set,heuristic 0 bruteforce 1.", ArgParseArgument::INTEGER));
    setDefaultValue(parser, "searching", "0");
    addOption(parser, ArgParseOption("i", "isomorphism", "To check whether two given graphs are isomorphic"));
    addOption(parser, ArgParseOption("c", "comparative", "File containing comparative graph", ArgParseArgument::INPUT_FILE,"IN"));
    addOption(parser, ArgParseOption("od", "odimension", "Specified initial dimension of original graph by user", ArgParseArgument::INTEGER));
    setDefaultValue(parser, "odimension", "3");
    addOption(parser, ArgParseOption("cd", "cdimension", "Specified initial dimension of comparative graph by user", ArgParseArgument::INTEGER));
    setDefaultValue(parser, "cdimension", "3");
    addOption(parser, ArgParseOption("ad", "directory", "test for all graphs in a specified directory", ArgParseArgument::STRING));
    addOption(parser, ArgParseOption("v", "verbose", "control the level of output files ", ArgParseArgument::INTEGER));
    setDefaultValue(parser, "verbose", "0");
    addSection(parser, "Output Option");
    addOption(parser, ArgParseOption("r", "result", "File containing canonical label of input graphs", ArgParseArgument::OUTPUT_FILE,"OUT"));
}

// --------------------------------------------------------------------------
// Function _parseOptions()
// --------------------------------------------------------------------------

template <typename TOption>
ArgumentParser::ParseResult _parseOptions(TOption & options,
                                          int argc,
                                          char const ** argv)
{
    // Setup ArgumentParsre.
    ArgumentParser parser("sgip");
    _setupParser(parser);

    // Parse commandline.
    ArgumentParser::ParseResult res=parse(parser,argc,argv);

    // Only extract options if the program will continue after parse().
    if(res != ArgumentParser::PARSE_OK)
        return res;
        
    // Extract option value.
    getOptionValue(options.originalFile, parser, "o");
    if (isSet(parser, "i"))
    {
        if (!isSet(parser, "c"))
        {
            if (!isSet(parser, "ad"))
            {
                std::cerr << "sgip" << ":comparative file has not been specified!" << std::endl;
                printShortHelp(parser, std::cerr);
                return ArgumentParser::PARSE_ERROR;
            }
        }
        options.isoCheck = true;
        getOptionValue(options.comparFile, parser, "c");
    }
    if (isSet(parser, "r"))
    {
        options.isPrintFile = true;
        getOptionValue(options.outputFile, parser, "r");
    }
    if (isSet(parser, "od"))
        getOptionValue(options.odimension, parser, "od");
    if (isSet(parser, "cd"))
        getOptionValue(options.cdimension, parser, "cd");
    if (isSet(parser, "a"))
        getOptionValue(options.algorithm, parser, "a");
    if (isSet(parser, "v"))
        getOptionValue(options.verbose, parser, "v");
    if (isSet(parser, "s"))
    {
        int s;
        getOptionValue(s, parser, "s");
        options.searchingType = static_cast<SearchingType>(s);
    }
    printVersion(parser, std::cerr);
    return ArgumentParser::PARSE_OK;
}

// --------------------------------------------------------------------------
// Function main()
// --------------------------------------------------------------------------

// Program entry point.
int main(int argc, char const ** argv)
{
    // Initial options and time variables.
    SgipOption options;
    time_t start, end;
    double dif;
    int ret = 0;
    time(&start);

    // Setup parser and parse the commandline.
    ArgumentParser::ParseResult res = _parseOptions(options, argc, argv);
    if(res != ArgumentParser::PARSE_OK)
        return res == ArgumentParser::PARSE_ERROR;

    // Finally, launch the program.
    if (!options.isAllatOnce)
        ret = _sgip(options);
    time(&end);
    dif = difftime(end, start);
    if (options.verbose > 2)
        std::cout << std::setprecision(10) << "escaped time:" << dif << "seconds" << std::endl;
    return ret;
}