lecture11graphs3 - Lecture 11: Graphs3 1) Dynamic...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 11: Graphs3 1) Dynamic equivalence relations 2) DF spanning trees ++
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Managing disjoint sets Disjoint sets of integers (vertices?) All integers in the same set are  equivalent in some way – connected Denote set Sk by its smallest element k For any integer (vertex) j and set Sk, we  want a fast answer to is j in Sk? We also want fast union of Sk U Sm
Background image of page 2
Managing disjoint sets S7={7, 9, 15} S3={3, 5} Start at any  node of parent- linked tree Follow links to  root 7 15 9 7 3 5
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Simple vector implementation Vertex  0. . 3 . . 5 . . 7. . 9 . .15 Parent -1  -1    3   -1  7     7 This is a parent-linked tree stored in a  single array or vector class DisjointSets { private:  vector<int> S; public:  DisjointSets( int numElts);          // make all singleton sets            int find (int x) const;                  // return root of tree for x            void union(int root1, int root2);   // union 2 sets }
Background image of page 4
find operation find(9) returns 7 find(7) returns 7 find(5) returns 3
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Union of disjoint sets S7={7, 9, 15} S3={3, 5} S3 U S7 is  created by  linking 7 to 3 EASY! Now find of 7,  9, 15 return 3 7 15 9 7 3 5
Background image of page 6
Notes on member functions Custom to denote set by smallest elt,  but not essential Union: More efficient to make shallow  tree a subtree of deeper tree, so keep  track of height Path compression: whenever doing a  find(x), link every node on the path  directly to the root to flatten the tree
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Flattening the tree with find Find(8)  changes the  parent pointer  of 8 to the  parent pointer  of 5, which  could recurse  even further 3 5 8 3 5 8
Background image of page 8
find and find with path  compression // return root of tree of x int find(int x) const {    if (S[x]<0) return x;    else return find(S[x]); } // with path compression int find(int x) const {    if (S[x]<0) return x;    else return          S[x]=find(S[x]); }
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Use in Kruskal’s Alg Computing the minimal spanning tree  by successive merging of components  via smallest unused edges that don’t  create a cycle
Background image of page 10
Recall Kruskal’s algorithm to  compute minimal spanning tree    How do we  know that  connecting  (2,3,5) creates  a cycle  whereas  connecting  (2,4,6) does  not? 3 6 2 4 9 2 3 5 5 6 Suppose the next smallest unused  edges are (2,3,5) then (2,4,6) 7 8
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Must check against cycles    How do we  know that  connecting  (2,3,5) creates  a cycle  whereas  connecting  (2,4,6) does  not?
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/25/2008 for the course CSE 331 taught by Professor M.mccullen during the Spring '08 term at Michigan State University.

Page1 / 49

lecture11graphs3 - Lecture 11: Graphs3 1) Dynamic...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online