Definitions
- A graph is said to be circuit-free iff it has no circuits
- A simple graph is called a tree iff it is circuit-free and connected
- Lemma 10.5.1: A non-trivial tree has at least one vertex of degree 1 (actually, it has at least two vertices of degree 1)
- Theorem 10.5.2: Any tree with vertices () has edges.
- Theorem 10.5.4: If is a connected graph with vertices and edges, then is a tree.
- A trivial tree is a tree that consists of a single vertex
- A simple graph is called a forest iff it is circuit free and not connected
- Terminal vertex or leaf & internal vertex: If tree has only one or two vertices, then each is called a terminal vertex. If has at least 3 vertices, then a vertex of degree 1 in is called a terminal vertex and a vertex of degree greater than 1 is called an internal vertex
Rooted Tree
- A rooted tree is a tree in which there is one vertex that is distinguished from the others and called the root.
- The level of a vertex is the number of deges along the unique path between it and the the root.
- The height of a rooted tree is the maximum level of any vertex of the tree.
- Children, Parents etc.
- The children of an internal vertex of a rooted tree are the vertices that are adjacent to and are one level further away from the root than v
- If is a child of , then is called the parent of
- Two distinct vertices that share the same parent are called siblings
- Given two distinct vertices and , if lies on the unique path between and the root, then is an ancestor of and is a descendant of
Binary Tree
A binary tree is a rooted tree in which every parent has at most two children. Each child is designated either a left child or a right child. The left subtree of vertex is the binary tree whose root is the left child, and consists of the left child and its descendants. (same for right subtree)
A full binary tree is a binary tree in which each parent has exactly two children.
- Theorem 10.6.2: For non-negative integers , if is any binary tree with height and terminal vertices, then (equivalently, )
Full binary tree theorem
If is a full binary tree with internal vertices, then has a total of vertices and has terminal vertices
Binary Tree Traversal
Breadth-first seach (BFS)
- Visit vertices level-by-level
- At each level, visit vertices from left to right
Depth-first seach (DFS)
- Pre-order
- Print the data of the root (or current vertex)
- Traverse the left subtree by recursively (pre-order)
- Traverse the right subtree by recursively (pre-order)
- In-order
- Traverse the left subtree recursively (in-order)
- Print the data of the root (or current vertex)
- Traverse the right subtree recursively (in-order)
- Post-order
- Traverse the left subtree recursively (post-order)
- Traverse the right subtree recursively (post-order)
- Print the data of the root (or current vertex)
- “Trick”:
- Pre-order: draw a dot on the left of the vertex, In-order: bottom between the branches, post-order: right
- Trace a “path” around the “perimeter” of the whole tree counter-clockwise (take the vertices as circles and edges to have thickness)
- Print the vertex data when we pass a dot
Spanning Trees
A spanning tree for a graph is a subgraph of that contains every vertex of and is a tree
- Proposition 10.7.1:
- Every connected graph has a spanning tree
- Any two spanning trees for a graph have the same number of edges
Minimum spanning tree
For a weighted graph (where each edge has a real number weight, and the total weight of all edges is the total weight of the graph), a minimum spanning tree for a connected weighted graph is a spanning tree that has the least possible total weight compared to all other spanning trees for the graph. Note: if is a weighted graph and is an edge of , then denotes the weight of and denotes the total weight of
Kruskal’s Algorithm (to get MST)
- Go through the edges of a graph in order of increasing weight
- For each edge, add it to what will become the minimum spanning tree, provided that it will not create a circuit
- After n-1 edges have been added, where n is the number of vertices of the graph, these edges, together with the vertices of the graph, form a minimum spanning tree for the graph
Note: If multiple edges have the same weight, then the minimum spanning tree might not be unique
Prim’s Algorithm (to get MST)
- Pick a vertex to start, and add it to the tree
- Find the next vertex to add to the tree by finding the vertex that is connected to the current tree by an edge with the lowest weight
i.e. keep adding the lowest weight vertex to the tree until all vertices have been added