Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
algo
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
Container Registry
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
Bertrand NÉRON
algo
Commits
4d90201c
Commit
4d90201c
authored
5 years ago
by
Bertrand Néron
Browse files
Options
Downloads
Patches
Plain Diff
add main to use Undirected and Directed weghted Graph
parent
e8438b35
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
Graph/main_3.py
+68
-39
68 additions, 39 deletions
Graph/main_3.py
with
68 additions
and
39 deletions
Graph/main_3.py
+
68
−
39
View file @
4d90201c
from
graph.graph_2
import
NDGraph
,
NDWGraph
,
Node
,
BFS
,
DFS
from
graph.graph_3
import
UnDirectedGraph
,
DirectedGraph
,
Node
,
Edge
from
graph.traversing
import
BFS
,
DFS
print
(
"
=============== UnDirected weight Graph =============
"
)
# We want to create a toy graph (not directed) to check our algos
# no
# / \
# n1 n3
# |
|
# |
/
|
# n2 n4
# \ /
# n5
nodes
=
[
Node
()
for
_
in
range
(
6
)]
g
=
NDGraph
(
nodes
)
g
=
UnDirectedGraph
()
g
.
add_edge
(
nodes
[
0
],
nodes
[
1
],
weight
=
1
)
g
.
add_edge
(
nodes
[
0
],
nodes
[
3
],
weight
=
3
)
g
.
add_edge
(
nodes
[
1
],
nodes
[
2
],
weight
=
2
)
g
.
add_edge
(
nodes
[
2
],
nodes
[
5
],
weight
=
5
)
g
.
add_edge
(
nodes
[
3
],
nodes
[
4
],
weight
=
4
)
g
.
add_edge
(
nodes
[
4
],
nodes
[
5
],
weight
=
6
)
g
.
add_edge
(
nodes
[
3
],
nodes
[
2
],
weight
=
15
)
print
(
"
BFS
"
)
for
n
,
p
in
BFS
(
g
,
nodes
[
0
]):
print
(
n
,
[
str
(
e
)
for
e
in
p
])
print
(
f
"
distance =
{
sum
([
e
.
weight
for
e
in
p
])
}
"
)
g
.
add_edge
(
nodes
[
0
],
(
nodes
[
1
],
nodes
[
3
]))
g
.
add_edge
(
nodes
[
1
],
(
nodes
[
2
],))
g
.
add_edge
(
nodes
[
2
],
(
nodes
[
5
],))
g
.
add_edge
(
nodes
[
3
],
(
nodes
[
4
],))
g
.
add_edge
(
nodes
[
4
],
(
nodes
[
5
],))
# no
# / \
# n1 n3
# | X |
# n2 n4
# \ /
# n5
g
.
add_edge
(
nodes
[
1
],
nodes
[
4
],
weight
=
20
)
# The graph is created we will test
print
(
"
Order
"
,
g
.
order
())
print
(
"
Size
"
,
g
.
size
())
# a Depth First Search
# starting from n0
# the possible solutions are
# n0, n1,n2,n5,n4,n3
# n0, n3, n4, n5, n2, n1
print
(
"
BFS
"
)
for
n
,
p
in
BFS
(
g
,
nodes
[
0
]):
print
(
n
,
[
str
(
e
)
for
e
in
p
])
print
(
f
"
distance =
{
sum
([
e
.
weight
for
e
in
p
])
}
"
)
print
(
"
DFS
"
)
for
n
in
DFS
(
g
,
nodes
[
0
]):
print
(
n
.
id
)
for
n
,
p
in
DFS
(
g
,
nodes
[
0
]):
print
(
n
,
[
str
(
e
)
for
e
in
p
])
print
(
f
"
distance =
{
sum
([
e
.
weight
for
e
in
p
])
}
"
)
# a Breadth First Search
# starting n0
# the possible solutions are
# n0, n1, n3, n2, n4, n5
# n0, n3, n1, n2, n4, n5
# n0, n1, n3, n4, n2, n5
# ....
print
(
"
BFS
"
)
for
n
,
_
in
BFS
(
g
,
nodes
[
0
]):
print
(
n
.
id
)
print
(
"
=============== Directed weighted Graph =============
"
)
nodes
+=
[
Node
()
for
_
in
range
(
6
)]
g
=
DirectedGraph
()
g
.
add_edge
(
nodes
[
6
],
nodes
[
7
],
weight
=
1
)
g
.
add_edge
(
nodes
[
7
],
nodes
[
8
],
weight
=
2
)
g
.
add_edge
(
nodes
[
8
],
nodes
[
11
],
weight
=
5
)
g
.
add_edge
(
nodes
[
11
],
nodes
[
10
],
weight
=
4
)
g
.
add_edge
(
nodes
[
6
],
nodes
[
9
],
weight
=
6
)
g
.
add_edge
(
nodes
[
9
],
nodes
[
10
],
weight
=
7
)
g
.
add_edge
(
nodes
[
8
],
nodes
[
9
],
weight
=
8
)
g
.
add_edge
(
nodes
[
7
],
nodes
[
10
],
weight
=
9
)
g
.
add_edge
(
nodes
[
10
],
nodes
[
7
],
weight
=
10
)
nodes
=
[
Node
()
for
_
in
range
(
6
)]
g
=
NDWGraph
(
nodes
)
# n6
# / \
# v V
# n7 n9
# |^ ^|
# | X |
# v vv
# n8 n10
# \ ^
# v/
# n11
g
.
add_edge
(
nodes
[
0
],
nodes
[
1
],
1
)
g
.
add_edge
(
nodes
[
0
],
nodes
[
3
],
3
)
g
.
add_edge
(
nodes
[
1
],
nodes
[
2
],
2
)
g
.
add_edge
(
nodes
[
2
],
nodes
[
5
],
5
)
g
.
add_edge
(
nodes
[
3
],
nodes
[
4
],
4
)
g
.
add_edge
(
nodes
[
4
],
nodes
[
5
],
5
)
print
(
"
Order
"
,
g
.
order
())
print
(
"
Size
"
,
g
.
size
())
print
(
"
BFS
"
)
path_len
=
0
for
n
,
w
in
BFS
(
g
,
nodes
[
0
]):
path_len
+=
w
print
(
n
,
w
,
path_len
)
for
n
,
p
in
BFS
(
g
,
nodes
[
6
]):
print
(
n
,
[
str
(
e
)
for
e
in
p
])
print
(
f
"
distance =
{
sum
([
e
.
weight
for
e
in
p
])
}
"
)
print
(
"
DFS
"
)
for
n
,
p
in
DFS
(
g
,
nodes
[
6
]):
print
(
n
,
[
str
(
e
)
for
e
in
p
])
print
(
f
"
distance =
{
sum
([
e
.
weight
for
e
in
p
])
}
"
)
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