Skip to content

Commit 20f413d

Browse files
author
Oleg Sh
committed
Enhance MaxClique algorithm: add handling for empty cliques and improve node connection checks. Update test cases.
1 parent f7bfda3 commit 20f413d

17 files changed

+217
-6
lines changed

.vscode/settings.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"files.associations": {
3-
"vector": "cpp"
3+
"vector": "cpp",
4+
"mutex": "cpp",
5+
"xlocinfo": "cpp",
6+
"xstring": "cpp",
7+
"shared_mutex": "cpp",
8+
"xhash": "cpp",
9+
"algorithm": "cpp",
10+
"execution": "cpp"
411
}
512
}

algorithm/MaxClique.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@ bool MaxClique::Calculate()
198198
FindMaxClique(m_param_algorithm_type);
199199
}
200200

201+
// If no clique found, we return clique with one vertex.
202+
if (m_max_clique.empty())
203+
{
204+
if (m_pGraph->GetNodesCount() > 0 && m_param_expected_size != 0)
205+
{
206+
m_max_clique.emplace_back(m_pGraph->GetNode((IndexType)0));
207+
}
208+
else if (m_param_expected_size == 0)
209+
{ // If expected size is 0, we return empty clique.
210+
m_max_clique.clear();
211+
}
212+
}
213+
201214
if (m_max_clique.size() != m_param_expected_size && m_param_expected_size != m_index_type_no_value)
202215
{
203216
LOG_WARNING("Max Clique size " << m_max_clique.size() << " is "
@@ -295,7 +308,7 @@ void MaxClique::UnitTest() const
295308
{
296309
if (u != v)
297310
{
298-
if (not m_pGraph->AreNodesConnected(v, u))
311+
if (! AreNodesConnected(v, u))
299312
{
300313
oss << "Invalid Clique! No edge between " << v << " and " << u;
301314
throw std::runtime_error(oss.str().c_str());
@@ -693,7 +706,7 @@ std::vector<ObjectId> MaxClique::VertexNeighbours(ObjectId v, const std::vector<
693706

694707
for (ObjectId u : neighbours)
695708
{
696-
if (m_pGraph->AreNodesConnected(v, u))
709+
if (AreNodesConnected(v, u))
697710
{
698711
new_neighbours.emplace_back(u);
699712
}
@@ -716,7 +729,7 @@ std::vector<MaxClique::ColourType> MaxClique::SortByGreedyColours(std::vector<Ob
716729
{
717730
ColourType colour = 0;
718731
while (std::any_of(colour_classes[colour].begin(), colour_classes[colour].end(),
719-
[&v, this](ObjectId u) { return m_pGraph->AreNodesConnected(u, v); }))
732+
[&v, this](ObjectId u) { return AreNodesConnected(u, v); }))
720733
{
721734
colour++;
722735
}

algorithm/MaxClique.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class MaxClique : public BaseAlgorithm
6868
std::vector<ObjectId> VertexNeighbours(ObjectId v, const std::vector<ObjectId> &neighbours);
6969
std::vector<ColourType> SortByGreedyColours(std::vector<ObjectId> &vertices);
7070

71+
/* Nodes are connected if there is non-direct edge or 2 directed edges */
72+
bool AreNodesConnected(ObjectId u, ObjectId v) const
73+
{
74+
return m_pGraph->AreNodesConnected(u, v) && m_pGraph->AreNodesConnected(v, u);
75+
}
76+
7177
std::vector<ObjectId> m_vertices;
7278
mutable std::vector<NodesEdge> m_max_clique_edges;
7379

test/MaxClique/empty.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
5+
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
6+
<graph id="G" edgedefault="directed">
7+
</graph>
8+
</graphml>
9+
10+
<!--
11+
n0>n1
12+
-->

test/MaxClique/empty.xml.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Result is 0 ()

test/MaxClique/graph2.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
5+
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
6+
<graph id="G" edgedefault="directed">
7+
<node id="n0"/>
8+
<node id="n1"/>
9+
</graph>
10+
</graphml>
11+
12+
<!--
13+
n0 n1
14+
-->

test/MaxClique/graph2.xml.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Result is 1 (n0)

test/MaxClique/graph2_direct.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
5+
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
6+
<graph id="G" edgedefault="directed">
7+
<node id="n0"/>
8+
<node id="n1"/>
9+
<edge source="n0" target="n1"/>
10+
</graph>
11+
</graphml>
12+
13+
<!--
14+
n0>n1
15+
-->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Result is 1 (n0)

test/MaxClique/graph5_direct.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
5+
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
6+
<graph id="G" edgedefault="directed">
7+
<node id="n0"/>
8+
<node id="n1"/>
9+
<node id="n2"/>
10+
<node id="n3"/>
11+
<node id="n4"/>
12+
<edge source="n0" target="n1"/>
13+
<edge source="n1" target="n2"/>
14+
<edge source="n2" target="n1"/>
15+
<edge source="n2" target="n3"/>
16+
<edge source="n3" target="n0"/>
17+
<edge source="n0" target="n4"/>
18+
</graph>
19+
</graphml>
20+
21+
<!--
22+
n4
23+
| <--
24+
n0>n1->n2>n3
25+
\ /
26+
<----
27+
-->

0 commit comments

Comments
 (0)