Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CSCI30_Project
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Marc Yap
CSCI30_Project
Commits
9541f220
Commit
9541f220
authored
Nov 28, 2019
by
Temujin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
degree and relationship method added
parent
46c29998
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
10 deletions
+54
-10
family_tree.py
family_tree.py
+54
-10
No files found.
family_tree.py
View file @
9541f220
...
...
@@ -4,9 +4,6 @@
TODO:
Find a way to get isolated nodes.
current structure:
bin tree, nodes in a dict
Problems to consider --
What to do if not rooted?
...
...
@@ -25,6 +22,16 @@ Is there only one root?
Total nodes of the tree?
11 generations = 2^11 - 1
Tree setup procedure:
1 get all the nodes and edges, check if they're valid.
2 Make sure there's only one root.
3 Find and set the root
4 Set the generations
5 run precompute method
6 done
"""
...
...
@@ -147,23 +154,48 @@ class FamilyTree:
visited
.
append
(
rc
.
get_name
())
self
.
dfs
(
rc
.
get_name
(),
*
funcs
)
def
slow_find_lca
(
self
,
a
,
b
):
def
lca
(
self
,
name1
,
name2
):
"""Find the lowest common ancestor of two nodes.
TODO: when one is the ancestor of the other
"""
return
self
.
slow_lca
(
name1
,
name2
)
def
slow_lca
(
self
,
name1
,
name2
):
"""Find the lca slowly.
two pointer method
Two pointer method.
"""
return
p1
=
self
.
family_tree
[
name1
]
p2
=
self
.
family_tree
[
name2
]
def
lca
(
self
,
a
,
b
):
assert
(
p1
is
not
None
)
assert
(
p2
is
not
None
)
# Make sure they have the same generation
while
p1
.
get_generation
()
<
p2
.
get_generation
():
p2
=
p2
.
get_parent
()
while
p2
.
get_generation
()
<
p1
.
get_generation
():
p1
=
p1
.
get_parent
()
# Keep moving up at the same time until they're equal
while
p1
is
not
p2
:
p1
=
p1
.
get_parent
()
p2
=
p2
.
get_parent
()
return
p1
.
get_name
()
def
fast_lca
(
self
,
a
,
b
):
"""Find the lca fast.
Reduction to range min. query
"""
return
def
degree
(
self
,
a
,
b
):
def
degree
(
self
,
p1
,
p2
):
"""Find the degree between two cousins."""
pass
l_ancestor
=
self
.
lca
(
p1
,
p2
)
return
min
([
self
.
lca
(
p1
,
l_ancestor
),
self
.
lca
(
p2
,
l_ancestor
)])
def
relationship
(
self
,
p1
,
p2
):
"""Determine the relationship between two distinct people in the tree.
...
...
@@ -261,19 +293,29 @@ class Person:
def
main
():
"""For testing."""
r"""For testing.
A
/
\
B C
/
\
D E
"""
ftree
=
FamilyTree
()
A
=
Person
(
"A"
,
69
,
True
)
B
=
Person
(
"B"
,
69
,
True
)
C
=
Person
(
"C"
,
69
,
True
)
D
=
Person
(
"D"
,
69
,
True
)
E
=
Person
(
"E"
,
666
,
False
)
ftree
.
insert_person
(
A
)
ftree
.
insert_person
(
B
)
ftree
.
insert_person
(
C
)
ftree
.
insert_person
(
D
)
ftree
.
insert_person
(
E
)
B
.
set_parent
(
A
)
C
.
set_parent
(
A
)
D
.
set_parent
(
B
)
E
.
set_parent
(
B
)
ftree
.
find_root
()
ancestors
=
ftree
.
get_ancestors
(
D
.
get_name
())
descendants
=
ftree
.
get_descendants
(
A
.
get_name
())
...
...
@@ -283,6 +325,8 @@ def main():
ftree
.
precompute
()
for
key
,
node
in
ftree
.
family_tree
.
items
():
print
(
"{}: {}"
.
format
(
node
,
node
.
get_generation
()))
print
(
"The lca of {} and {} is {}"
.
format
(
D
,
C
,
ftree
.
lca
(
"D"
,
"C"
)))
print
(
"The lca of {} and {} is {}"
.
format
(
B
,
D
,
ftree
.
lca
(
"B"
,
"D"
)))
if
__name__
==
"__main__"
:
...
...
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