1 CSCI 104 Graph Representation and Traversals Mark Redekopp David Kempe Sandra Batista
2 GRAPH REPRESENTATIONS
3 Graph Notation • A graph is a collection of vertices h (or nodes) and edges that b c connect vertices a A vertex d V E g An edge a (a,c) e b (a,e) f c (b,h) d (b,c) • Let V be the set of vertices e (c,e) • Let E be the set of edges • f (c,d) Let |V| or n refer to the number of vertices g (c,g) • Let |E| or m refer to the h (d,f) number of edges (e,f) |V|=n=8 |E|=m=11 (f,g) (g,h)
4 Graphs in the Real World • Social networks • Computer networks / Internet • Path planning • Interaction diagrams • Bioinformatics
5 Basic Graph Representation • Can simply store edges in a list – Unsorted – Sorted h V E b c a (a,c) a b (a,e) d c (b,h) g d (b,c) e (c,e) e f f (c,d) g (c,g) h (d,f) (e,f) |V|=n=8 |E|=m=11 (f,g) (g,h)
6 Graph ADT • What operations would you want to perform on a graph? • addVertex() : Vertex • addEdge(v1, v2) • getAdjacencies(v1) : List<Vertices> – Returns any vertex with an edge from v1 to itself • removeVertex(v) • removeEdge(v1, v2) • edgeExists(v1, v2) : bool #include<iostream> using namespace std; template <typename V, typename E> class Graph{ Perfect for templating the data associated with a vertex and edge as V and E };
7 More Common Graph Representations • Graphs are really just a list of lists h b – List of vertices each having their own list of c adjacent vertices a • Alternatively, sometimes graphs are also d g represented with an adjacency matrix – Entry at (i,j) = 1 if there is an edge between e vertex i and j, 0 otherwise f a b c d e f g h c,e a a 0 0 1 0 1 0 0 0 List of Vertices c,h b Adjacency Lists b 0 0 1 0 0 0 0 1 a,b,d,e,g c c 1 1 0 1 1 0 1 0 c,f d d 0 0 1 0 0 1 0 0 a,c,f e e 1 0 1 0 0 1 0 0 d,e,g f f 0 0 0 1 1 0 1 0 c,f,h g g 0 0 1 0 0 1 0 1 h b,g h 0 1 0 0 0 0 1 0 How would you express this Adjacency Matrix Representation using the ADTs you've learned?
8 Graph Representations • Let |V| = n = # of vertices and h b |E| = m = # of edges c • Adjacency List Representation a – O(_______________) memory storage d – Existence of an edge requires O(_____________) time g • Adjacency Matrix Representation – O(_______________) storage e – f Existence of an edge requires O(_________) lookup a b c d e f g h c,e a a 0 0 1 0 1 0 0 0 List of Vertices c,h b Adjacency Lists b 0 0 1 0 0 0 0 1 a,b,d,e,g c c 1 1 0 1 1 0 1 0 c,f d d 0 0 1 0 0 1 0 0 a,c,f e e 1 0 1 0 0 1 0 0 d,e,g f f 0 0 0 1 1 0 1 0 c,f,h g g 0 0 1 0 0 1 0 1 h b,g h 0 1 0 0 0 0 1 0 How would you express this Adjacency Matrix Representation using the ADTs you've learned?
9 Graph Representations • Let |V| = n = # of vertices and |E| = m = # of edges h b • Adjacency List Representation c – O(|V| + |E|) memory storage a – Define degree to be the number of edges incident on a vertex ( deg(a) d = 2, deg(c) = 5, etc. g – Existence of an edge requires searching the adjacency list in O(deg(v)) • Adjacency Matrix Representation e – O(|V| 2 ) storage f – Existence of an edge requires O(1) lookup (e.g. matrix[i][j] == 1 ) a b c d e f g h c,e a a 0 0 1 0 1 0 0 0 List of Vertices c,h b Adjacency Lists b 0 0 1 0 0 0 0 1 a,b,d,e,g c c 1 1 0 1 1 0 1 0 c,f d d 0 0 1 0 0 1 0 0 a,c,f e e 1 0 1 0 0 1 0 0 d,e,g f f 0 0 0 1 1 0 1 0 c,f,h g g 0 0 1 0 0 1 0 1 h b,g h 0 1 0 0 0 0 1 0 Adjacency Matrix Representation
10 Graph Representations • Can 'a' get to 'b' in two hops? h b • Adjacency List c – For each neighbor of a… a – Search that neighbor's list for b d g • Adjacency Matrix – Take the dot product of row a & column b e f a b c d e f g h c,e a a 0 0 1 0 1 0 0 0 c,h b b 0 0 1 0 0 0 0 1 a,b,d,e,g c c 1 1 0 1 1 0 1 0 c,f d d 0 0 1 0 0 1 0 0 a,c,f e e 1 0 1 0 0 1 0 0 d,e,g f f 0 0 0 1 1 0 1 0 c,f,h g g 0 0 1 0 0 1 0 1 h b,g h 0 1 0 0 0 0 1 0 Adjacency Matrix Representation
11 Graph Representations • Can 'a' get to 'b' in two hops? h b • Adjacency List c – For each neighbor of a… a – Search that neighbor's list for b d g • Adjacency Matrix – Take the dot product of row a & column b e f int sum = 0; for(int i=0; i < n; i++){ sum += adj[src][i]*adj[i][dst]; a b c d e f g h } if(sum > 0) // two-hop path exists a 0 0 1 0 1 0 0 0 c,e a b 0 0 1 0 0 0 0 1 c,h b c 1 1 0 1 1 0 1 0 a,b,d,e,g c d 0 0 1 0 0 1 0 0 c,f d e 1 0 1 0 0 1 0 0 a,c,f e f 0 0 0 1 1 0 1 0 d,e,g f g 0 0 1 0 0 1 0 1 c,f,h g h 0 1 0 0 0 0 1 0 h b,g Adjacency Matrix Representation
12 Directed vs. Undirected Graphs • In the previous graphs, edges were undirected (meaning h b edges are 'bidirectional' or 'reflexive') c – An edge (u,v) implies (v,u) a • In directed graphs, links are unidirectional d – An edge (u,v) does not imply (v,u) g – For Edge (u,v): the source is u, target is v • e For adjacency list form, you may need 2 lists per f vertex for both predecessors and successors Target a b c d e f g h c,e a a 0 0 1 0 1 0 0 0 List of Vertices h b Adjacency Lists b 0 0 0 0 0 0 0 1 b,d,e,g c c 0 1 0 1 1 0 1 0 f d Source d 0 0 0 0 0 1 0 0 f e e 0 0 0 0 0 1 0 0 f f 0 0 0 0 0 0 0 0 f g g 0 0 0 0 0 1 0 0 h g h 0 0 0 0 0 0 1 0 Adjacency Matrix Representation
13 Directed vs. Undirected Graphs h • In directed graph with edge (src,tgt) we define b – Successor(src) = tgt c a – Predecessor(tgt) = src • d Using an adjacency list representation may g warrant two lists predecessors and successors e f Target c,e a a b c d e f g h List of Vertices h c b a 0 0 1 0 1 0 0 0 b,d,e,g a c b 0 0 0 0 0 0 0 1 f c d c 0 1 0 1 1 0 1 0 f a,c Source e d 0 0 0 0 0 1 0 0 d, e, g f e 0 0 0 0 0 1 0 0 f c,h g f 0 0 0 0 0 0 0 0 h g b g 0 0 0 0 0 1 0 0 h 0 0 0 0 0 0 1 0 Succs Preds (Outgoing) (Incoming) Adjacency Matrix Representation
14 Graph Runtime, |V| = n, |E| =m Operation vs Add edge Delete Edge Test Edge Enumerate Implementation edges for single for Edges vertex Unsorted array or Linked List Sorted array Adjacency List Adjacency Matrix
15 Graph Runtime, |V| = n, |E| =m Operation vs Add edge Delete Edge Test Edge Enumerate Implementation edges for single for Edges vertex Unsorted array Θ (1) Θ (m) Θ (m) Θ (m) or Linked List Sorted array Θ (m) Θ (m) Θ (log m) Θ (log [if binary search m)+ Θ (deg(v)) used] [if binary search used] Adjacency List Time to find List Time to find List Time to find List Time to find List for a given for a given vertex for a given for a given vertex + Θ (1) + Θ (deg(v)) vertex + vertex + Θ (deg(v)) Θ (deg(v)) Adjacency Θ (1) Θ (1) Θ (1) Θ (v) Matrix
16 A graph with restrictions TREES
17 Tree Definitions – Part 1 • Definition : A connected, acyclic (no cycles) graph with: Terms: – A root node, r, that has 0 or more subtrees • – Parent(i) : Node directly Exactly one path between any two nodes above node i • In general: • Child(i) : Node directly below – Nodes have exactly one parent (except for the root which node i has none) and 0 or more children • Siblings : Children of the • d-ary tree same parent – Tree where each node has at most d children • Root: Only node with no – Binary tree = d-ary Tree with d=2 parent • Leaf: Node with 0 children • Height: Number of nodes on parent root longest path from root to any Left child Right child leaf siblings Ancestor • Subtree(n) : Tree rooted at A 3-ary node n (trinary) • Ancestor(n) : Any node on tree the path from n to the root • Descendant(n): Any node in Leaf Descendant the subtree rooted at n subtree
18 Tree Definitions – Part 2 • Tree height: maximum # of nodes on a path from root to any leaf • Full d-ary tree, T, where Full – Every vertex has 0 or d children and all leaf nodes are at the same level (i.e. adding 1 more node requires increasing the height of the tree) • Complete d-ary tree Complete, but not full – Top h-1 levels are full AND bottom level is filled left-to-right – Each level is filled left-to-right and a new level is not started until the previous one is complete • Balanced d-ary tree – Tree where, for EVERY node, the subtrees for each child differ in height by at most 1 Full DAPS, 6 th Ed. Figure 15-8 Complete, but not full
19 Tree Height • A full or complete binary tree of n nodes has height, h= 𝑚𝑝 2 (𝑜 + 1) – This implies the minimum height of any tree with n nodes is 𝑚𝑝 2 (𝑜 + 1) • The maximum height of a tree with n nodes is, ___ 15 nodes => height log 2 (16) = 4 5 nodes => height = __
20 Array-based and Link-based TREE IMPLEMENTATIONS
Recommend
More recommend