Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
aim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Mareš
aim
Commits
e697468d
Commit
e697468d
authored
10 years ago
by
Ladislav Láska
Browse files
Options
Downloads
Patches
Plain Diff
aim-comp - hledani komponent grafu
parent
d0c5b56a
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
aim-comp/Makefile
+14
-0
14 additions, 0 deletions
aim-comp/Makefile
aim-comp/comp.c
+112
-0
112 additions, 0 deletions
aim-comp/comp.c
aim-comp/makegraph.pl
+28
-0
28 additions, 0 deletions
aim-comp/makegraph.pl
with
154 additions
and
0 deletions
aim-comp/Makefile
0 → 100644
+
14
−
0
View file @
e697468d
all
:
gcc
-o
comp
-g
-std
=
c11
-Wall
-Wextra
comp.c
clean
:
rm
-f
comp
samples
:
./makegraph.pl 100 1
>
small.g
samples-evil
:
./makegraph.pl 10000000 1
>
big.g
./makegraph.pl 10000000 2
>
extra.g
.PHONY
:
all clean
This diff is collapsed.
Click to expand it.
aim-comp/comp.c
0 → 100644
+
112
−
0
View file @
e697468d
/******************************************************************************
* Filename: comp.c
*
* Created: 2015/04/26 20:56
* Author: Ladislav Láska
* e-mail: laska@kam.mff.cuni.cz
*
******************************************************************************/
#include
<string.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<assert.h>
struct
vertex
{
struct
vertex
**
neighbours
;
int
comp
;
};
struct
graph
{
int
n
;
struct
vertex
*
vertices
;
};
struct
graph
*
graph_load
(
char
*
file
)
{
FILE
*
f
=
fopen
(
file
,
"r"
);
if
(
!
f
)
{
printf
(
"Could not read input file.
\n
"
);
goto
fail
;
}
struct
graph
*
g
=
calloc
(
1
,
sizeof
(
struct
graph
));
/* First line contains number of vertices. */
if
(
1
!=
fscanf
(
f
,
"%i
\n
"
,
&
g
->
n
))
{
fprintf
(
stderr
,
"Format error: Invalid header.
\n
"
);
goto
fail
;
}
g
->
vertices
=
calloc
(
g
->
n
,
sizeof
(
struct
vertex
));
/* Other lines contain it's number, neighbour count and their numbers. */
for
(
int
i
=
0
;
i
<
g
->
n
;
i
++
)
{
int
id
,
neighbours
;
if
(
2
!=
fscanf
(
f
,
"%i %i"
,
&
id
,
&
neighbours
))
{
fprintf
(
stderr
,
"Format error: Invalid line %i.
\n
"
,
i
);
goto
fail
;
}
if
(
id
==
g
->
n
)
{
fprintf
(
stderr
,
"Found vertex #%i, but only %i vertices expected!
\n
"
,
id
,
g
->
n
);
goto
fail
;
}
g
->
vertices
[
id
].
neighbours
=
calloc
(
neighbours
+
1
,
sizeof
(
struct
vertex
*
));
for
(
int
j
=
0
;
j
<
neighbours
;
j
++
)
{
int
vref
;
if
(
1
!=
fscanf
(
f
,
"%i"
,
&
vref
))
{
fprintf
(
stderr
,
"Format error: Invalid edge %i at vertex %i.
\n
"
,
j
,
i
);
goto
fail
;
}
if
(
vref
>=
g
->
n
)
{
fprintf
(
stderr
,
"Format error: Invalid reference to nonexistent vertex %i.
\n
"
,
vref
);
goto
fail
;
}
g
->
vertices
[
id
].
neighbours
[
j
]
=
&
g
->
vertices
[
vref
];
assert
(
g
->
vertices
[
id
].
neighbours
[
j
+
1
]
==
NULL
);
}
}
fclose
(
f
);
return
g
;
fail:
if
(
g
)
{
if
(
g
->
vertices
)
{
for
(
int
i
=
0
;
i
<
g
->
n
;
i
++
)
{
if
(
g
->
vertices
[
i
].
neighbours
)
free
(
g
->
vertices
[
i
].
neighbours
);
}
free
(
g
->
vertices
);
}
free
(
g
);
}
return
NULL
;
}
void
mark_component
(
struct
vertex
*
v
,
int
comp
)
{
assert
(
v
);
v
->
comp
=
comp
;
for
(
int
i
=
0
;
v
->
neighbours
[
i
]
!=
NULL
;
i
++
)
{
if
(
v
->
neighbours
[
i
]
->
comp
)
continue
;
mark_component
(
v
->
neighbours
[
i
],
comp
);
}
}
int
find_components
(
struct
graph
*
g
)
{
int
comp
=
0
;
for
(
int
i
=
0
;
i
<
g
->
n
;
i
++
)
{
if
(
g
->
vertices
[
i
].
comp
)
continue
;
comp
++
;
mark_component
(
&
g
->
vertices
[
i
],
comp
);
}
return
comp
;
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
2
)
{
fprintf
(
stderr
,
"Usage: ./comp graph_file.g"
);
exit
(
1
);
}
struct
graph
*
g
=
graph_load
(
argv
[
1
]);
if
(
g
)
{
fprintf
(
stderr
,
"Processing graph '%s' on %i vertices.
\n
"
,
argv
[
1
],
g
->
n
);
printf
(
"Found %i components.
\n
"
,
find_components
(
g
));
}
return
0
;
}
This diff is collapsed.
Click to expand it.
aim-comp/makegraph.pl
0 → 100755
+
28
−
0
View file @
e697468d
#!/usr/bin/perl
use
common::
sense
;
use
Data::
Dumper
;
use
List::
Util
qw(reduce)
;
my
(
$nodes
,
$adeg
)
=
@ARGV
;
die
"
Usage: ./makegraph.pl num_nodes avg_deg.
"
unless
defined
$nodes
&&
defined
$adeg
;
srand
(
42
);
my
@graph
;
my
$th
=
$nodes
/
$adeg
;
for
(
my
$i
=
0
;
$i
<
$nodes
;
$i
++
)
{
for
(
my
$j
=
int
(
rand
(
$th
));
$j
<
$nodes
;
$j
+=
1
+
int
(
rand
(
$th
)))
{
$graph
[
$i
]
->
{
$j
}
=
1
;
$graph
[
$j
]
->
{
$i
}
=
1
;
}
}
print
"
$nodes
\n
";
for
(
my
$i
=
0
;
$i
<
$nodes
;
$i
++
)
{
my
$sum
=
scalar
keys
%
{
$graph
[
$i
]};
print
"
$i
$sum
"
.
join
("
",
keys
%
{
$graph
[
$i
]})
.
"
\n
";
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment