Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
competitive
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
Bruno Freitas Tissei
competitive
Commits
bcb0c48f
Commit
bcb0c48f
authored
6 years ago
by
Bruno Freitas Tissei
Browse files
Options
Downloads
Patches
Plain Diff
Improve Articulations/Bridges comments
Signed-off-by:
Bruno Freitas Tissei
<
bft15@inf.ufpr.br
>
parent
63373508
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
algorithms/graph/articulations_bridges.cpp
+28
-8
28 additions, 8 deletions
algorithms/graph/articulations_bridges.cpp
with
28 additions
and
8 deletions
algorithms/graph/articulations_bridges.cpp
+
28
−
8
View file @
bcb0c48f
...
...
@@ -6,9 +6,15 @@
*/
vector
<
int
>
graph
[
MAX
];
int
visited
[
MAX
],
parent
[
MAX
];
int
cont
[
MAX
],
parent
[
MAX
];
int
low
[
MAX
],
L
[
MAX
];
// The level of a node x (L[x]) is the "height" of x relative to the root
// of the DFS
int
L
[
MAX
];
// The low-link value of a node x (low[x]) is defined as the smallest level
// of another node reachable from x when doing a DFS
int
low
[
MAX
];
// Answer vector with bridges (edges)
vector
<
ii
>
bridges
;
...
...
@@ -22,24 +28,35 @@ vector<int> articulations;
*/
void
dfs
(
int
x
)
{
int
child
=
0
;
cont
[
x
]
=
1
;
visited
[
x
]
=
1
;
for
(
auto
i
:
graph
[
x
])
{
if
(
!
cont
[
i
])
{
if
(
!
visited
[
i
])
{
child
++
;
parent
[
i
]
=
x
;
// Initially low[i] is equal to low[x]
low
[
i
]
=
L
[
i
]
=
L
[
x
]
+
1
;
dfs
(
i
);
// After the DFS, low[i] might have changed, in that case, low[x]
// should be updated if a lower value has been found
low
[
x
]
=
min
(
low
[
x
],
low
[
i
]);
if
((
parent
[
x
]
==
-
1
&&
child
>
1
)
||
(
parent
[
x
]
!=
-
1
&&
low
[
i
]
>=
L
[
x
]))
// If x is the root and has more than one child, or if it's not the
// root and the child i of x has no back edges, then x is an
// articulation vertex
if
((
parent
[
x
]
==
-
1
&&
child
>
1
)
||
(
parent
[
x
]
!=
-
1
&&
low
[
i
]
>=
L
[
x
]))
articulations
.
pb
(
x
);
// If node i can't reach a node above x (has no back edges), then it
// the egde (x,i) is a bridge
if
(
low
[
i
]
>
L
[
x
])
bridges
.
pb
(
ii
(
x
,
i
));
// If i has been visited but it's not x's parent, then i is "above"
// (has smaller level then x), meaning that the edge (x,i) is a "back edge"
}
else
if
(
parent
[
x
]
!=
i
)
low
[
x
]
=
min
(
low
[
x
],
L
[
i
]);
}
...
...
@@ -49,9 +66,12 @@ void dfs(int x) {
* Applies tarjan algorithm and format articulations vector.
* @param x root vertex
*/
void
tarjan
(
int
x
)
{
void
tarjan
(
int
n
)
{
mset
(
parent
,
-
1
);
dfs
(
x
);
for
(
int
i
=
0
;
i
<
n
;
++
i
)
if
(
!
visited
[
i
])
dfs
(
i
);
// Remove duplicates for articulations
sort
(
all
(
articulations
));
...
...
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