This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Graphs and Trees Graphs Graphs Graphs When we talk about a mathematical graph, we are When talking about a "picture" containing points (called nodes) and lines or arrows (called arcs) nodes and arcs connecting various nodes to each other. When lines are used, they are called undirected graphs. undirected graphs When arrows are used they called directed graphs. directed graphs an undirected graph containing 5 nodes and 5 arcs Graphs Graphs When we talk about a mathematical graph, we are When talking about a "picture" containing points (called nodes) and lines or arrows (called arcs) nodes and arcs connecting various nodes to each other. When lines are used, they are called undirected graphs. undirected When arrows are used they called directed graphs. directed graphs
a directed graph containing 5 nodes and 5 arcs Graphs Graphs When we talk about a mathematical graph, we are When talking about a "picture" containing points (called nodes) and lines or arrows (called arcs) nodes and arcs connecting various nodes to each other. When lines are used, they are called undirected graphs. undirected graphs When arrows are used they called directed graphs. directed graphs
a directed graph containing 5 nodes and 5 arcs Graphs Graphs Nodes are sometimes called vertices. Nodes vertices Two nodes are adjacent if they are Two adjacent connected by an arc. connected A loop is any arc that has both ends at the loop same node. same Parallel arcs are arcs that have the same arcs endpoints as one another. endpoints A simple graph is one that has no loops or simple graph parallel arcs. parallel Graphs Graphs
An isolated node is one that is not adjacent isolated to any other node. to The degree of a node is the number of arc The degree ends at that node. In a directed graph, the indegree is the number of arrows pointing indegree to the node; the outdegree is the number of outdegree arrows pointing from the node. arrows A complete graph is one for which every complete graph pair of nodes is connected by an arc. pair Graphs Graphs A subgraph is any subset of a graph. subgraph A path from node a to node b is a sequence path of arcs that leads from node a to node b. The length of a path is the number of arcs The length in the path in A graph is connected if there is a path from graph connected any node to any other node. any A cycle is a path starting at and ending at cycle the same node. Graphs Graphs
Find two nodes that are not adjacent a1
2 Find a node adjacent to itself Find a loop Find two parallel arcs Find the degree of node 3
3 1 a6
5 a2 a5
4 a3 Find a path of length 5 Find a cycle Is this graph complete? Is this graph connected? a4 Graphs Graphs A bipartite complete graph is one in which bipartite the nodes can be divided into two subsets and every node in the first subset is connected to every node in the other subset. connected K3,2 Graphs Graphs What would a complete graph with five What vertices look like? vertices K5 Graphs Graphs Two graphs are said to be isomorphic if their Two isomorphic pictures are just "twisted versions of one another." In other words, they have the same number of In nodes (with a onetoone correspondence between the nodes of each), the same number of arcs, and that two nodes are connected in one graph if and only if the corresponding nodes in the other graph are connected. nodes Graphs Graphs Representing Graphs Representing In order to represent a graph in a computer, In there are a number of techniques used. there Adjacency matrix Adjacency list Graphs Graphs
0 1 1 0 0 3 1 a1
2 1 0 0 a6
5 a2 a5
4 a3 0 0 0 0 1* 0 0 1* 0 1 0 0 1 1 1 0 0 a4 Graphs Graphs
2
1 3 a1
2 1 1 4 4 a6
5 a2 a5
4 etc.
3 a3 a4 Trees Trees A tree is an acyclic, connected graph with tree one node designated as the root of the tree. root The book shows trees as being undirected, The but most real implementations use directed graphs. graphs. A tree is a directed, acyclic connected graph tree with one node called the root having no other root
nodes pointing to it. All other nodes have exactly one node pointing to it. exactly Trees Trees Even the directed graph definition is not Even particularly useful. particularly Note that all the longest path in a tree must Note start at the root (Why?) and must end at a node that points to no other node. node A node that points to no other nodes is node called a leaf. The node that points to a leaf The given node is called its parent. The nodes parent The pointed to by a node are called its children. children Trees Trees A node that points to no other nodes is node called a leaf. The node that points to a leaf The given node is called its parent. The nodes parent The pointed to by a node are called its children. children We generalize this by saying that if there is We a path from node A to node B, that node A is that an ancestor of node B, and that node B is a and descendant of node A. A. Trees Trees A node that points to no other nodes is node called a leaf. The node that points to a leaf The given node is called its parent. The nodes parent The pointed to by a node are called its children. children We generalize this by saying that if there is We a path from node A to node B, that node A is that an ancestor of node B, and that node B is a ancestor and descendant of node A. descendant A. Trees Trees
The longest path found in a tree is called the height of the tree. We will define the height length of a path as the number of nodes in the path. the The root of a tree is said to be at level 0. level Its children are at level 1, and so on. level Trees Trees Now let's get real (unlike the textbook) Most of the time we deal with what are Most called binary trees. binary trees A binary tree is a tree for which two binary tree children are defined (the left child and the left child right child.) right child The left child and the right child don't have to exist for each node. A leaf has no leaf children. children. Binary Trees Binary Why are binary trees so interesting? Why many applications
• • • decision trees decision expression trees expression search trees search can pack a lot of information in very few levels Binary Trees Binary
Now, the important stuff. Now, A tree is an object that is either empty (no tree empty nodes) or consists of a node and two subtrees (called the left subtree and the subtrees left subtree right subtree.) right subtree This is a recursive definition!!!! This Binary Trees Binary The recursive nature of trees makes them an ideal medium for searching for information. ideal lots of information in very few levels a (binary) tree of height 2 can contain up to 22 – 1 nodes nodes a (binary) tree of height 10 can contain up to 210 – 1 nodes. (approximately 1000) nodes. a (binary) tree of height 20 can contain up to 220 – 1 nodes. (approximately 1,000,000) nodes. Binary Trees Binary
We can build a tree of height n in such a way that we can find any piece of information in up to n comparisons. information comparisons. That would be a maximum of 20 comparisons to look through a tree with about a million pieces of information!!! about Binary Trees Binary
Let's talk recursion. Let's What is the height of a tree? What Binary Trees Binary The height of an empty tree is 0. The height of all other trees is 1 more than the larger of the heights of the left and right subtrees. right
5 ? ?
1 2 4 ?
3 1 1 2 1 Binary Trees Binary height(t) = if t is empty height(t) then 0 otherwise otherwise max(height(left(t)), height(right(t))) + 1 Binary Trees Binary Define the size of a tree to be the number of Define size nodes in that tree. nodes Define size recursively. What is the size of an empty tree? 0 1 + size(left subtree) + size(right subtree) What is the size of a nonempty tree? Binary Trees Binary The height of an empty tree is 0. The height of all other trees is 1 more than the larger of the heights of the left and right subtrees. right
9 ?
5 ?
1 3 ?
4 1 1 2 1 Binary Trees Binary How do we process all the nodes of a binary tree? The tree is nonlinear, so there isn't just one possible The way. way. postorder traversal inorder traversal preorder traversal process left subtree,root right left, right, then subtree and finally, the root process left subtree, then the root left, the right subtree and finally, root, right process root, then the left subtree, and root, left, right finally the right subtree Binary Trees Binary Traverse this tree. Traverse
A B D E G C F H I Binary Trees Binary When are these various traversals useful? postorder: getting rid of a tree preorder: building a tree inorder: printing a binary search tree Binary Trees Binary Binary Search Tree: Build a tree in the following way:
• at all times, and for the tree and all its subtrees, the values in the left subtree are all less than the value at the root; the values in the right subtree are all greater than (or equal to) the value at the root. (or • Look at an example Look Binary Trees Binary Binary Search Tree Binary
6 3 1 5 10 8 12 14 18 Binary Trees Binary How would you build such a tree? How
6 10 1 3 5 8 12 14 let's add 10 to this tree. 10 < 12 >6 8 gogo down to down to the tright he left
18 but wait, it's empty! Binary Trees Binary What happens if you do an inorder traversal? traversal?
6 3 1 5 10 8 12 14 18 Binary Expression Trees Binary
+ D A F H * / I D + A*(F – H/I) Huffman Codes Huffman Data compression Data what is JPEG? what JPEG
• a file containing compressed information about a picture. about how can data be compressed?
• one way is through what are called Huffman Codes. Huffman Huffman Codes Huffman
Ordinarily, each character, or color, would occupy one memory cell or byte (8 bits). occupy Suppose a picture only contains 14 colors Suppose We could hold each color in 4 bits We A 1024 by 768 picture would occupy about 400,000 bytes. 400,000 Huffman Codes Huffman
Suppose we had a code that could differentiate between all the colors AND use fewer bits for the common colors. use Imagine a picture that is mostly white, with a fair amount of red, and a little blue and yellow and no other colors. We would code white with one BIT, red with maybe two or three, blue with maybe three or four and the others with a lot. three On the average, fewer bits would be used. Huffman Codes Huffman
A technique to do this is called Huffman coding. coding. What does this have to do with graphs or What trees? trees? Let's do an example and we'll see. Let's Huffman Codes Huffman
character Frequency a 48 cg k p ? 9 12 4 17 10 4 9 10 12 17 48 sort frequencies from low to high choose the two smallest values Trees Trees
10 13 12 17 9
22 48 12 4 + 9 = 13 4 10 9 12 17 48 48 remove 4 and 9 replace with 13 resort 10 + 12 = 22 remove 10 and 12 replace with 22 resort 4
13 4 9 17 10 Trees Trees
13 4 9 22 10 12 13 17 10 30 17 22 12 48 48
0 100
1 48 a
0 52
1 22
0 a0 c 1101 g 101 k 1100 p 111 ? 100 1 30
0 1 4
48 22 10 12 52 9
30 13 17 10 ? 12 g 13
0 1 17 p 4 9 4 k 9c Huffman Codes Huffman
character Frequency a 48%
1 bit c g k p ? 10%
3 9% 12% 4% 17%
4 3 4 3 .48 + .09x4 + .12x3 + .04x4 +.17x3 + .1x3 = 2.17 We need an average of 2.17 bits per character For six characters, we would ordinarily need 3 bits per character. Huffman Codes Huffman Why does this work? In a binary tree, there is a unique path from the In unique root to any node. root Homework revision: 5.4/1, 2, 58 Graphs and Algorithms Graphs Directed Graphs and Binary Relations Directed
Every directed graph implies a particular binary relation where an arc from node a to node b relation represents the binary relation element (a, b) represents In other words, a directed graph is a visual representation of a binary relation on nodes. representation Graphs and Algorithms Graphs Such a directed graph or binary relation can be described by an adjacency matrix: adjacency
a d 1 0 0 0 0 0 0 0 0 1 01 01 00 00 01 0 0 1 0 1 b c e R = {(a,a), (a,d), (b,d), (c,e), (e,b), (e,d), (e,e)} Graphs and Algorithms Graphs Reachability Reachability Node x is reachable from node y if there is a reachable path from y to x. Graphs and Algorithms Graphs Boolean multiplication of matrices Boolean The same as regular multiplication but we use or (r ) instead of + and and (r ) instead of r p or p and p Consider the boolean product below. What is Consider the result at row 2 column 3? the (0 p 0) p (1 p 1) p (0 p 1) = 0 p 1 p 0 = 1 1 0 0 1 1 0 0 0 1 x 100 111 001 = 1 1 0 1 1 0 1 1 1 Graphs and Algorithms Graphs Reachability Reachability
Node x is reachable from node y if there is a reachable path from y to x. x. If M is the n x n Boolean adjacency matrix for directed graph D, then the boolean “square” of then M, M2, represents all pairs of nodes that are reachable by a path of length 2. reachable x is reachable from y if and only if if M o M2 o M3 o M4 o .... o Mn p p p p p contains a 1 in row y, column x. column Graphs and Algorithms Graphs Reachability Reachability
x is reachable from y if and only if if M o M2 o M3 o M4 o .... p Mn p p p p contains a 1 in row y, column x. column notice the amount of computation necessary: notice n  1 matrix multiplications and n  1 matrix additions. additions. each multiplication involves more than n r n o p p each n separate computations separate Graphs and Algorithms Graphs Reachability Reachability each multiplication involves more than n r p each non p separate computations separate There is a method for drastically reducing the amount of There computation necessary. It is called Warshall's Algorithm. Algorithm Warshall's Algorithm Warshall's Compute a series of matrices in the following manner: Let the Adjacency Matrix be matrix M0 Let In order to compute matrix Mk+1, only look at In column k + 1 of Mk . column For each row in column k + 1 that is a 0, For that copy that row to Mk+1 For each row in column k + 1 that is a 1, that “or” that row with row k + 1 and copy that THAT row to Mk+1 THAT Stop when the matrix no longer changes. Stop Warshall's Algorithm Warshall's Matrix Mk represents the reachability by paths of length up to k + 1 going to all the nodes up through node k Graphs and Algorithms Graphs
01 00 10 00 10 0 1 0 0 1 00 00 10 00 00
2 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1
3 0 0 1 0 0 0 0 0 0 0 M0
1 5 4 M1 k=1 Graphs and Algorithms Graphs
01 00 11 00 11 0 1 0 0 1 00 00 10 00 00
2 0 0 1 0 1 1 0 1 0 1 1 1 1 0 1
3 0 0 1 0 0 0 0 0 0 0 M1
1 5 4 M2 k=2 Graphs and Algorithms Graphs
01 00 11 00 11 1 1 1 0 1 00 00 10 00 00
2 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1
3 1 1 1 0 1 0 0 0 0 0 M2
1 5 4 M3 k=3 Graphs and Algorithms Graphs
Homework 6: 6.1/1, 2, 13, 17, 18, 23, 24 ...
View
Full
Document
This note was uploaded on 10/13/2010 for the course MATH MATH 2255 taught by Professor Landis during the Spring '10 term at Fairleigh Dickinson.
 Spring '10
 landis
 Math

Click to edit the document details