Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
algo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Bertrand NÉRON
algo
Commits
4d90201c
Commit
4d90201c
authored
Sep 06, 2019
by
Bertrand Néron
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✨
add main to use Undirected and Directed weghted Graph
parent
e8438b35
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
39 deletions
+68
-39
Graph/main_3.py
Graph/main_3.py
+68
-39
No files found.
Graph/main_3.py
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
])
}
"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment