Download presentation
Presentation is loading. Please wait.
Published byGervais Parker Modified over 9 years ago
1
ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392
2
2 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.
3
3 Example (Spanning Trees) v w x y z vw x y z vw x y z v w x y z A graph G Spanning Trees
4
4 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
5
5 Formal definition of minimum spanning tree Given a connected undirected graph G. Let T be a spanning tree of G. cost(T) = e T weight(e) The minimum spanning tree is a spanning tree T which minimizes cost(T) v1v1 v4v4 v3v3 v5v5 v2v2 5 2 3 7 8 4 Minimum spanning tree
6
6 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; }
7
7 Prim’s algorithm (I) Start from v 5, find the minimum edge attach to v 5 v2v2 v1v1 v4v4 v3v3 v5v5 5 2 3 7 8 4 Find the minimum edge attach to v 3 and v 5 v2v2 v1v1 v4v4 v3v3 v5v5 5 2 3 7 8 4 Find the minimum edge attach to v 2, v 3 and v 5 v2v2 v1v1 v4v4 v3v3 v5v5 5 2 3 7 8 4 v2v2 v1v1 v4v4 v3v3 v5v5 5 2 3 7 8 4 v2v2 v1v1 v4v4 v3v3 v5v5 5 2 3 7 8 4 Find the minimum edge attach to v 2, v 3, v 4 and v 5
8
8 Minimum Spanning Trees Minimum Spanning Trees Given a weighted undirected graph, compute the spanning tree with the minimum cost Given a weighted undirected graph, compute the spanning tree with the minimum cost
9
9
10
Discrete Mathematical Structures: Theory and Applications 10 Rooted Tree
11
CSE 2813 Discrete Structures 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
12
CSE 2813 Discrete Structures Example ( x + y ) 2 + ( x -3)/( y +2) + x y 2 – x 3 + y 2 / +
13
CSE 2813 Discrete Structures Infix Notation + – + / + 2 x y x 3 y 2 Traverse in inorder adding parentheses for each operation x + y () 2 () + x – 3() / y + 2() () ()
14
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
15
CSE 2813 Discrete Structures Prefix Notation (Polish Notation) Traverse in preorder x + y 2 + x – 3 / y + 2 + – + / + 2 x y x 3 y 2
16
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
17
CSE 2813 Discrete Structures 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 Evaluating Postfix Notation
18
CSE 2813 Discrete Structures Postfix Notation (Reverse Polish) Traverse in postorder x + y 2 + x – 3 / y + 2 + – + / + 2 x y x 3 y 2
19
CSE 2813 Discrete Structures Example 3 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 +
20
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, degree of v: no. of incident edges to v
21
21
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.