ECS110 Lecture Notes for Monday, October 30'th 1995
Professor Rogaway Conducting
Lecture 13
Reminder:
- Midterm #1 & Programing Assignment #1 Will be returned Wednsday.
- Try the EXERCISE mentioned in the class regarding Traversal of a Binary Tree.
Previous Lecture Topic:
Next Lecture Topic:
- Illustrations of Today's Topic using algorithms
Today:
- Graph Theory Terminology
- Defining Graph
- graph G is a non empty, finite set of vertecies V and a set of 2
element subset of V denoted by E called nodes.
G=(V,E)
- Adjacent Members
- Two Vertices are Adjacent when they share an edge.
- Degree of Vertex V
- This is the number of vertices that are adjacent to this node.
![[Figure 1. showing Edge to a vertex]](edge.gif)
- Path
- a path in G is a sequence (V1,..........Vn) such that (V1,V2),(V2,V3), ..... ,
(Vn-1,Vn) are all edges in the set E.
![[Figure2. showing Path Pictorially]](path.gif)
- Being Connected
- When every 2 vertices lie on the same common path, they are said to be connected.
An example of a connected Graph is a Tree.
- Cycle
- A cycle is a path from (V1.....Vn) where V1=Vn for n>3.
![[Figure 3. Demonstrating Cyclical nodes]](cycle.gif)
- Acyclical
- A graph is acyclic if none of the paths traveled return back to the first vertex.
An example of an Acyclic graph is a Tree.
- Trees:
- General Trees
- - Defining a Tree
- A tree is a iconnected, acyclic graph
- A forest is a set of trees
![[Figure 4. A tree vs. Not a tree]](tree.gif)
- - Rooted Trees
- This type of tree, also known as Oriented Tree, is a tree where one
vertex of which is distinguished as - The Root. This root is
usually placed at the top of the tree. A rooted tree can therefore be
even represented using arrows extending from the root as can be seen
in Figure 5. Also in Figure six, both trees represent the same thing, a
root with a degree of 5.
T=(V,E,r) where r, the root, is an element of V
(meaning root is the vertex)
![[Figure 5. Representation of a Rooted Tree using Arrows]](figure5.gif)
- - Leaf
- This is defined as a node which has a degree of one. In other words, this node
bears no children. See Figure 7.
![[Figure7. illustrates a Leaf Node]](figure7.gif)
- - Internal Node
- This is a node with a degree of n =>2, found between edges.
![[Figure8. Illustrates an Internal Node]](figure8.gif)
- - Level
- The root is assigned level 1. The level is the depth of the branch division.
(Like generations).
![[Figure9. Shows the levels in a tree]](figure9.gif)
- - Subtree
- In describing trees we describe branches going out from a vertex as a
subtree. In particular in Rooted Trees we refer to them as the children
where the parent is the vertex where the links are extending from.
- - Ordered Tree
- This is an oriented tree where the siblings are numbered 1, 2, .... , n in the
ascending order of age (where the left hand side sibling is the youngest
and the right hand side sibling is the oldest. So this is a method of organizing
our tree in a way that siblings are easily distinguished to be younger or
older than the other.
![[Figure 10. illustrates what an ordered tree looks like]](figure10.gif)
- Binary Trees
- - Defining a Binary Tree
- A finite set of nodes which is either empty, or has a distinguished node
called the the Root, and two disjoint binary trees: A Left binary
subtree , a Right binary subtree. A child in the left subtree is not the
same as having the child in the right subtree as illustrated by Figure 11.
![[Figure 11. illustrates a binary tree]](figure11.gif)
- - Complete Tree
- A tree is complete when every level i (except the last) has all 2^i
nodes at level i filled from Left to Right.
![[Figure 12. illustrates a complete VS an incomplete tree]](figure12.gif)
- - Relationship Between Depth and number of Nodes
- A binary tree of depth d has at most (2^d)-1 nodes.
- The Depth of a COMPLETE tree is the upper limit of log(n+1) where the base
of the log is 2.
- - Relationship Between Number of Leafs and Missing Nodes
- Number of Missing leafs == Number of Nodes +1
- - Representation of Binary Trees
- As Arrays
- Each box with in the array (starting with 1 as the root), can be
algorithmically given the appropiate nodes. Let's use this
algorithm as a general way of placing the member with in an
array:
Given a node i :
Parent(i) = Lower Limit of (i/2)
Left Child(i) = 2*i
RightChild(i) = (2*i)+1
![[Figure 13. illustrates what how an array holds a binary tree]](figure13.gif)
- As Linked Lists
- Abstractly Speaking, the Link list members will contain
important informations:
- Pointer to the Left Child
- Pointer to the Right Child
- What value is Stored Here.
![[Figure 14. illustrates what how alinked list holds a binary tree]](figure14.gif)
- The efficiency Factor for an array representation
- - For a complete Tree
- Uses just enough space to fill up its array (hence the
term complete!). Traversal to a child or parent is easily
done
algorithmically.
- - For an incomplete Tree
- Excessive space can be wasted this way if too many
parents do not have complete set of children.
- Ease of Use
- Arrays tend to be easier to use since we can find where a child
or a parent is by simply calculating it. In an array we also have
the added advantage of knowing where the root parent is
at all time (for the ease of use, we use box 1 as root). On the
other hand, a linked list, if not explicitly stated, needs explicit
addressing to a child who wants to go back to a parent (or
a root).
- - Translating Any Ordered Forest into Binary Tree form
- This can be used to translate forests into binary tree format. Traversing down
the tree, form a new binary tree by the following criteria:
- Use the Left node of the new binary tree member to save
the location of the youngest child from the forest.
- Use the Right node of the new binary tree member to
save the location of the older sibling from the forest.
![[Figure 15. illustrates changing from a forest to a binary tree]](figure15.gif)
- - traveling through a Binary Tree
- Traversing
- Traveling through the binary tree, this is to visit each node EXACTLY once
- Exploring
- This is traveling a tree and visiting each node
at least Once
- EXCERCISE Traversal
- Write a procedure for traversing the maze below: