2
Graph Theory 3 Trees. A tree is a graph that is connected and has no cycles. A rooted tree is a tree where one of the vertices has been selected to be the “root”. The vertices that are connected by an edge to the root are its children. Note: a vertex can have more than two children. The children of the root are the level 1 vertices of the tree. The children of the children are the level 2 vertices of the tree. And so on. A leaf vertex is a vertex that has no children. The height of a rooted tree is the length of the longest path from the root to a leaf. Consider the tree below with vertices labeled A,B,C,D,E,F,G. Rooted Trees on the computer. A rooted tree can be represented by a list. The first item in the list is the root. The remaining items in the list are sub–lists. One sub–list for each sub–tree of the root. Each sub–list is a list for a tree (the sub–tree); So each sub–list starts with the root of its sub–tree. The tree above corresponds to the list: Lst = [A, [B, [E]], [C], [D, [F], [G]]] SubGraphs. Spanning Trees. Minimal Spanning Trees. A subgraph of a graph, G, is a graph made from some of the vertices and some of the edges of G. If an edge is in the subgraph, then the vertices it touches need to be in the subgraph also. Let G be a graph. A spanning tree for G is a subgraph that is a tree (connected, no cycles) and contains all the vertices of G. Let G be a weighted graph. The cost of a spanning tree is the sum of the weights on its edges. A minimal spanning tree for G a spanning tree whose cost is lowest among all spanning trees of G.

cs.indstate.educs.indstate.edu/sternfl/graph3.pdf · Created Date: 4/16/2020 3:02:35 PM

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: cs.indstate.educs.indstate.edu/sternfl/graph3.pdf · Created Date: 4/16/2020 3:02:35 PM

Graph Theory 3

Trees. A tree is a graph that is connected and has no cycles. A rooted tree is a tree where one of thevertices has been selected to be the “root”. The vertices that are connected by an edge to the root are itschildren. Note: a vertex can have more than two children. The children of the root are the level 1 verticesof the tree. The children of the children are the level 2 vertices of the tree. And so on. A leaf vertex is avertex that has no children. The height of a rooted tree is the length of the longest path from the root to aleaf. Consider the tree below with vertices labeled A,B,C,D,E,F,G.

Rooted Trees on the computer. A rooted tree can be represented by a list. The first item in the listis the root. The remaining items in the list are sub–lists. One sub–list for each sub–tree of the root. Eachsub–list is a list for a tree (the sub–tree); So each sub–list starts with the root of its sub–tree. The treeabove corresponds to the list:

Lst = [A, [B, [E]], [C], [D, [F], [G]]]

SubGraphs. Spanning Trees. Minimal Spanning Trees.

A subgraph of a graph, G, is a graph made from some of the vertices and some of the edges of G. If an edgeis in the subgraph, then the vertices it touches need to be in the subgraph also.

Let G be a graph. A spanning tree for G is a subgraph that is a tree (connected, no cycles) and contains allthe vertices of G.

Let G be a weighted graph. The cost of a spanning tree is the sum of the weights on its edges. A minimal

spanning tree for G a spanning tree whose cost is lowest among all spanning trees of G.

Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
A
Robert Sternfeld
B
Robert Sternfeld
C
Robert Sternfeld
D
Robert Sternfeld
E
Robert Sternfeld
F
Robert Sternfeld
G
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Page 2: cs.indstate.educs.indstate.edu/sternfl/graph3.pdf · Created Date: 4/16/2020 3:02:35 PM

Prim’s Algorithm.

Input: A connected, weighted graph, G = (VG, EG) whereVG is the set of verticesEG is the set of edges.

Output: A minimal spanning tree for G.

Strategy: Build the spanning tree, S = (VS , ES)a vertex and edge at a time.

Steps:

1. Pick a start vertex, call it v0. Set VS =[v0] and ES=[ ]2. while (VG 6= VS):3. consider each edge that connects a vertex already in VS to a vertex, vi, NOT in SV .

Choose such an edge with the lowest costAppend the edge to ES

Append vi to VS

1. Writing trees as lists. Consider the first tree drawn in this webpage. Now add to the drawing: Adda vertex H under vertex C. Add a vertex I under vertex H. Connect vertex C to vertex H with an edge.Connect vertex H to vertex I with an edge. Now write the list for this tree.

2. Change root. Again consider the first tree on this webpage. Redraw the tree with vertex D as the root.(Help: Since vertex D has edges that connect it to vertices A, F, and G, these vertices will be the childrenof the new root D). Finish drawing tree with root D. Now write a list for this tree.

3. Given weighted graph G with vertices 0,1,2,3 and edges 0,1,2,3,4. That is, VG = [0, 1, 2, 3] and EG =[0, 1, 2, 3, 4]. The vertices and edges have the Incidence array and weights:

Incidence = [

[1,0,1,1,0],

[1,1,0,0,0],

[0,1,1,0,1],

[0,0,0,1,1]

]

wt = [5,1,3,4,2]

Recall that rows correspond to vertices and columns correspond to edges. The wt array says that edge 0 hasweight 5 and edge 1 has weight 1.

Start the algorithm with v0 = 0 and VS = [0] and VE = [ ]. Run the algorithm (by hand). Each repetitionof the loop should append a vertex to VS and an edge to ES . Write the list ES containing the edges asappended.

4. Explain in a few sentences why the graph S = (VS , ES) made by Prim’s algorithm does not have anycycles.