-
Notifications
You must be signed in to change notification settings - Fork 3
/
graph.cpp
120 lines (109 loc) · 3.07 KB
/
graph.cpp
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
//
// Created by yyh on 6/27/2019.
//
#include "graph.h"
#include <list>
//---------------------------------------VariantGraph---------------------------------------
VariantGraph::VariantGraph()
: visited(nullptr), hap_visited(nullptr), filtered(nullptr) {}
VariantGraph::VariantGraph(uint variant_count)
: variant_count(variant_count), hap_visited(nullptr)
{
visited = new bool[variant_count];
filtered = new bool[variant_count];
hap_visited = new bool[2 * variant_count];
for (uint i = 0; i < variant_count; i++)
{
visited[i] = false;
filtered[i] = false;
hap_visited[2 * i] = hap_visited[2 * i + 1] = false;
graph.emplace_back();
haplotype_graph.emplace_back();
haplotype_graph.emplace_back();
}
}
VariantGraph::~VariantGraph()
{
delete[] visited;
visited = nullptr;
delete[] hap_visited;
hap_visited = nullptr;
delete[] filtered;
filtered = nullptr;
graph.clear();
haplotype_graph.clear();
connected_component.clear();
}
void VariantGraph::load_connected_component()
{
for (uint i = 0; i < variant_count; i++)
{
if (visited[i]) // if a variant is part of connected component, just skip
continue;
if (check_disconnected_variant(i))
copy_uoset2set(i); // a disjointed variant, can be accessed with starting index (key of map)
else
copy_insert_connected_component(i); //connected component, can be accessed with starting index (key of map)
}
}
void VariantGraph::copy_insert_connected_component(uint idx)
{
std::set<uint> dest;
dfs_util(dest, idx);
connected_component.emplace(idx, dest);
}
void VariantGraph::dfs_util(std::set<uint> &dest, uint idx)
{
if (visited[idx] || filtered[idx])
return;
auto ¤t_variant = graph[idx];
visited[idx] = true;
for (auto i : current_variant)
{
if (filtered[i])
continue;
dest.insert(i);
}
for (auto i : current_variant)
dfs_util(dest, i);
}
//call this function after find connected component
bool VariantGraph::fully_seperatable(uint idx)
{
uint s = 2 * idx;
uint d = 2 * idx + 1;
std::list<uint> queue;
hap_visited[s] = true;
queue.push_back(s);
while (!queue.empty())
{
s = queue.front();
queue.pop_front();
for (auto i : haplotype_graph[s])
{
if (this->filtered[s/2])
continue;
if (i == d)
return false;
if (!hap_visited[i])
{
hap_visited[i] = true;
queue.push_back(i);
}
}
}
return true;
}
void VariantGraph::split_after_filtering(uint idx, std::vector<std::set<uint>> &blks)
{
std::set<uint> &sub_graph_variant = this->connected_component[idx];
for (auto &i : sub_graph_variant)
visited[i] = false;
for (auto &i : sub_graph_variant)
{
if (visited[i] || filtered[i])
continue;
blks.emplace_back();
dfs_util(blks.back(), i);
}
}