240 likes | 458 Views
Graph Theory (Trees) . هواللطیف. ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392. Spanning Trees. Let G be a connected graph. A spanning tree in G is a subgraph of G that includes all the vertices of G and is also a tree. The edges of the tree are called branches . v. v. w. w. x.
E N D
Graph Theory(Trees) هواللطیف ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392
Spanning Trees Let G be a connected graph. A spanning tree in G is a subgraph of G that includes all the vertices of G and is also a tree. The edges of the tree are called branches.
v v w w x x y z y z v w x y z v w x y z Example (Spanning Trees) Spanning Trees A graph G
Minimum Spanning Tree • Consider a connected undirected graph where • Each node x represents a country x • Each edge (x, y) has a number which measures the cost of placing telephone line between country x and country y • Problem: connecting all countries while minimizing the total cost • Solution: find a spanning tree with minimum total weight, that is, minimum spanning tree
v2 v3 v1 2 5 Minimum spanning tree 4 3 7 v5 8 v4 Formal definition of minimum spanning tree • Given a connected undirected graph G. • Let T be a spanning tree of G. • cost(T) = eTweight(e) • The minimum spanning tree is a spanning tree T which minimizes cost(T)
Prim’s algorithm (II) Algorithm PrimAlgorithm(v) • Mark node v as visited and include it in the minimum spanning tree; • while (there are unvisited nodes) { • find the minimum edge (v, u) between a visited node v and an unvisited node u; • mark u as visited; • add both v and (v, u) to the minimum spanning tree; }
v2 v2 v2 v2 v2 v1 v1 v1 v1 v1 2 2 2 2 2 v3 v3 v3 v3 v3 5 5 5 5 5 4 4 4 4 4 3 3 3 3 3 7 7 7 7 7 8 8 8 8 8 v4 v4 v4 v4 v4 v5 v5 v5 v5 v5 Start from v5, find the minimum edge attach to v5 Find the minimum edge attach to v3 and v5 Find the minimum edge attach to v2, v3 and v5 Find the minimum edge attach to v2, v3 , v4 and v5 Prim’s algorithm (I)
Minimum Spanning Trees • Given a weighted undirected graph, compute the spanning tree with the minimum cost
Rooted Tree Discrete Mathematical Structures: Theory and Applications
Representing Arithmetic Expressions • Complicated arithmetic expressions can be represented by an ordered rooted tree • Internal vertices represent operators • Leaves represent operands • Build the tree bottom-up • Construct smaller subtrees • Incorporate the smaller subtrees as part of larger subtrees CSE 2813 Discrete Structures
+ / + – 2 + y y x x 3 2 Example (x+y)2 + (x-3)/(y+2) CSE 2813 Discrete Structures
+ / + – 2 + y y x x 3 2 ( ( ( ( ( ( ) ) ) ) ) ) Infix Notation • Traverse in inorder adding parentheses for each operation y 2 x + + x – y 3 + 2 / CSE 2813 Discrete Structures
Evaluating Prefix Notation • In an prefix expression, a binary operator precedes its two operands • The expression is evaluated right-left • Look for the first operator from the right • Evaluate the operator with the two operands immediately to its right CSE 2813 Discrete Structures
+ / + – 2 + y y x x 3 2 Prefix Notation(Polish Notation) • Traverse in preorder y 2 x + + x – y 3 + 2 / CSE 2813 Discrete Structures
Example + / + 2 2 2 / – 3 2 + 1 0 + / + 2 2 2 / – 3 2 1 + / + 2 2 2 / 1 1 + / + 2 2 2 1 + / 4 2 1 + 2 1 3 CSE 2813 Discrete Structures
Evaluating Postfix Notation • In an postfix expression, a binary operator follows its two operands • The expression is evaluated left-right • Look for the first operator from the left • Evaluate the operator with the two operands immediately to its left CSE 2813 Discrete Structures
+ / + – 2 + y y x x 3 2 Postfix Notation(Reverse Polish) • Traverse in postorder y 2 x + + x – y 3 + 2 / CSE 2813 Discrete Structures
Example 2 2 + 2 / 3 2 – 1 0 + / + 4 2 / 3 2 – 1 0 + / + 2 3 2 – 1 0 + / + 2 1 1 0 + / + 2 1 1 / + 2 1 + 3 CSE 2813 Discrete Structures
Definitions • Vertex w is adjacent to vertex v if there is an edge (v,w). Given an edge e = (u,v) in an undirected graph, u and v are the endpoints of e and e is incident on u (or on v). In a digraph, u & v are the origin and destination. e leaves u and enters v. • A digraph or graph is weighted if its edges are labeled with numeric values. • In a digraph, • Out-degree of v: number of edges coming out of v • In-degree of v: number of edges coming in to v • In a graph, degreeof v:no. of incident edges to v