Rooted Trees – CMPT 225
Graphs
Graph is a pair G=<V,E>, with:
- V, a set called “vertacies” or “nodes”
- E, a set of pairs from V, i.e. called edges
- Example:
Example in table format:
node | connections |
---|---|
1 | 2,3 |
2 | 1 |
3 | 1 |
“G is directed”: edges are ordered pairs (often called arcs)
i.e. graph one:
node | connections |
---|---|
1 | 2,3 |
2 | |
3 |
does not equal graph two:
node | connections |
---|---|
1 | 3 |
2 | 1 |
3 |
“G is undirected” edges are sets
i.e. graph one:
node | connections |
---|---|
1 | 3,2 |
2 | 1 |
3 | 1 |
is equal to graph two:
node | connections |
---|---|
1 | 2,3 |
2 | 1 |
3 | 1 |
By default, by “graph” we will mean “undirected graph”.
Path
Path of G of length n is a sequence of vertacies s-t. are edges of G.
s-t Path in G: path in G.
Example of a s-t path of length 6 for G:
node | connections |
---|---|
a | b,s |
b | a,c |
c | b,d,e,s |
d | c,s |
e | c,f,g |
f | e,h |
g | e,h,t |
h | f,g,t |
s | a,c,d |
t | g,h |
Vertex t is reachable from s in G if ther is an s-t path in G. G is connected if for every pair , u is reachable from v.
Transcriber’s note: this is much easier to show with graphics and in fact very hard to show easily without graphics. I did my best, but it should be noted that this is very obvious with a visual graph. It becomes two seperate graphs, in essense. So for convenience, I will be splitting this up into two tables, even though technically it’s one “graph” with two sets of connected nodes.
Connected:
node | connections |
---|---|
a | b,s |
b | a,c |
c | b,d,e,s |
d | c,s |
e | c,f,g |
f | e,h |
g | e,h,t |
h | f,g,t |
s | a,c,d |
t | g,h |
Not connected:
node | connections |
---|---|
a | b,s |
b | a,c |
c | b,d,e,s |
d | c,s |
e | c |
s | a,c,d |
node | connections |
---|---|
f | h |
g | h,t |
h | f,g,t |
t | g,h |
Cycles & Trees
Cycle in G: path in G where
Simple path: all vertecies are distinct. Simple Cycle: cycle where is a simple path.
Simple cylcle of length 5: (see “connected” graph for confirmation):
Cycle of length 7 + repeats:
node | connections |
---|---|
a | b,s |
b | a,c,f |
c | b,d,e,f,s |
d | c,s |
e | c,f,g |
f | c,e,h,b |
g | e,h,t |
h | f,g,t |
s | a,c,d |
t | g,h |
Tree
Tree: a connected, acyclic graph:
Example 1 (not a tree, due to cycle: ):
node | connections |
---|---|
a | b |
b | a,c |
c | b,d,e,s |
d | c,s |
e | c,g |
f | h |
g | e,h,t |
h | f,g,t |
s | c,d |
t | g,h |
Example 2 (a tree, no cycle, all connected):
Now using ARIA trees:
-
s
-
a
- b
- d
-
c
-
e
-
f
- h
-
g
- i
-
f
-
e
-
a
Example 3 (not a tree) (transcriber’s note: written as two trees for simplicity’s sake)
-
s
-
a
- b
- d
-
c
- e
-
a
-
f
-
h
-
g
- i
-
g
-
h
Fact: every tree with verticies has edges.
Rooted-tree:
Rooted tree: tree with a distringuished vertex called the root.
Unrooted tree:
-
a
-
b
- c
- d
-
e
-
f
- g
- h
-
f
-
i
-
j
-
k
- l
-
k
-
j
-
b
Tree T with root:
-
a (root)
-
b
- c
- d
-
e
-
f
- g
- h
-
f
-
i
-
j
-
k
- l
-
k
-
j
-
b
An alternate drawing of T, with no semantic change.
Notice: in a tree there is a unique path between any two verticies. So: a unique path from any vertex to the root. Thus: the root indicates a direction on the edges e.g. towards the root.
A graph of the same tree, T, with arrows pointing towards the root. There is no reason for having any semantic markup for this.
(sometimes “away from the root”).
Rooted Tree Terminology
-
z (root, anscestor of a)
-
y
- x (leaf)
- w (leaf)
-
v (anscestor of a)
- u (leaf)
-
t (anscestor of a, parent of a)
- s (sibling of a, leaf)
- r (sibling of a, leaf)
-
a (descendant of a, anscestor of a)
-
q (child of a, desandant of a)
- o (decendant of a, leaf)
- n (decendant of a, leaf)
- m (decendant of a, leaf)
- p (child of a, desendant of a, leaf)
-
q (child of a, desandant of a)
-
y
- The root has no parent.
- Leaves have no children.
- Internal nodes are the non-leaves (sometimes root ??? [can’t read] too)
Depth & Height
-
4 (depth 0)
-
1 (depth 1)
- 0 (depth 2)
- 0 (depth 2)
-
3 (depth 1)
-
2 (depth 2)
-
1 (depth 3)
- 0 (depth 4)
- 0 (depth 3)
-
1 (depth 3)
- 0 (depth 2)
-
2 (depth 2)
-
1 (depth 1)
- Depth of node v = length of path from v to the root.
- Height of node v = length of longest path from to a decendant of v (e.g. to a leaf)
- Height of Tree T = height of its root = max height of any node in T = max depth of any node in T.
A rooted tree is:
- k-ary if no node has >k children.
- binary if no node has >2 children.
- ordered if the children of every node are ordered.
E.g. A ordered trenary tree:
-
no name
-
1
- 1
- 2
- 2
-
3
- 1
- 2
- 3
-
1
Notice: when we draw a tree, or represent it in a data structure, we order it.
In an ordered binary tree, every child of a node v is either the “left child of v” or the “right child of v”.
Transcriber’s note: nodes’ children are transcribes from left to right. So for a binary tree, the first child is the “left child” and the second child is the “right child”.
Subtree rooted at v
Subtree rooted at v: tree with root v and containing all decendants of v.
In a binary tree:
- “left subtree of v” means the subtree rooted at the left child of v.
- sim. for “right child of v”.
-
z
-
v (subtree rooted at v)
-
y (subtree rooted at v, left subtree of v)
- v (subtree rooted at v, left subtree of v
- u (subtree rooted at v, left subtree of v
-
x (subtree rooted at v, right subtree of v)
- w (subtree rooted at v, right subtree of v)
-
y (subtree rooted at v, left subtree of v)
-
a
- b
- c
-
v (subtree rooted at v)