Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
ogs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Mojtaba Abdolkhani
ogs
Commits
982c583d
Commit
982c583d
authored
8 years ago
by
Dmitri Naumov
Committed by
Christoph Lehmann
8 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[NL] Rewrite hasTopologicalOrdering().
Mainly clarifying the algorithm and micro-optimiziations.
parent
10e07dac
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
NumLib/NamedFunctionCaller.cpp
+40
-22
40 additions, 22 deletions
NumLib/NamedFunctionCaller.cpp
with
40 additions
and
22 deletions
NumLib/NamedFunctionCaller.cpp
+
40
−
22
View file @
982c583d
...
...
@@ -10,44 +10,62 @@
#include
"NamedFunctionCaller.h"
#include
<algorithm>
#include
<list>
#include
"BaseLib/uniqueInsert.h"
#include
"BaseLib/Error.h"
bool
hasTopologicalOrdering
(
std
::
vector
<
std
::
vector
<
int
>>
const
&
dependencies
)
/// To understand the working of the algorithm the following two lemmata are
/// useful:
/// If a directed graph has a topological order, then it is a Directed Acyclic
/// Graph (DAG).
/// If a graph is a DAG, then it has a node with no incoming edges.
///
/// \note Negative indices in the graph adjacency list are leaves of the graph.
///
/// \param graph represents directed graph given as an adjacency list.
/// \return true if the given directed graph is acyclic.
bool
hasTopologicalOrdering
(
std
::
vector
<
std
::
vector
<
int
>>
const
&
graph
)
{
std
::
vector
<
unsigned
>
dep_counts
(
dependencies
.
size
());
// Number of incoming edges for each node of the graph
std
::
vector
<
unsigned
>
number_incoming_edges
(
graph
.
size
());
//
init dependency counts
for
(
std
::
size_t
fct_idx
=
0
;
fct_idx
<
dependencies
.
size
();
++
fct_idx
)
//
Count all incoming edges (i,j) for each node (i).
for
(
auto
const
&
node_i_adjacencies
:
graph
)
{
for
(
int
dep
:
dependencies
[
fct_idx
])
{
if
(
dep
>=
0
)
++
dep_counts
[
dep
];
for
(
int
node_j
:
node_i_adjacencies
)
{
if
(
node_j
>=
0
)
// ignore negative indices.
++
number_incoming_edges
[
node_j
];
}
}
auto
num_dependent
=
dep_counts
.
size
();
std
::
list
<
std
::
size_t
>
q
;
// init work queue
for
(
std
::
size_t
fct_idx
=
0
;
fct_idx
<
dep_counts
.
size
();
++
fct_idx
)
// Working queue: a set of nodes with no incoming edges.
std
::
vector
<
std
::
size_t
>
q
;
for
(
std
::
size_t
node_i
=
0
;
node_i
<
number_incoming_edges
.
size
();
++
node_i
)
{
if
(
dep_counts
[
fct_idx
]
==
0
)
{
q
.
push_back
(
fct_idx
);
--
num_dependent
;
if
(
number_incoming_edges
[
node_i
]
==
0
)
{
q
.
push_back
(
node_i
)
;
}
}
auto
num_dependent
=
number_incoming_edges
.
size
()
-
q
.
size
();
while
(
!
q
.
empty
())
{
auto
const
fct_idx
=
q
.
front
();
q
.
pop_front
();
for
(
int
dep
:
dependencies
[
fct_idx
])
{
if
(
dep
<
0
)
continue
;
if
(
--
dep_counts
[
dep
]
==
0
)
{
q
.
push_back
(
dep
);
auto
const
node_i
=
q
.
back
();
q
.
pop_back
();
// Decrement counts for all edges (i,j).
for
(
int
node_j
:
graph
[
node_i
])
{
if
(
node_j
<
0
)
continue
;
// ignore negative indices
if
(
--
number_incoming_edges
[
node_j
]
==
0
)
{
// Add a node without incoming edges to the queue.
q
.
push_back
(
node_j
);
--
num_dependent
;
}
}
...
...
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