- 1. Sanders: Graphs 1 Graphs and Graph Representation INFORMATIK ¾ 1736 L. Euler asks about a “touristic” question: ¾ Street or Computer networks ¾ Railway connections (space and time) ¾ Citation or Coauthorship social networks ¾ Job precedences scheduling problems ¾ Values and arithmetic operations compiler construction ¾
- 2. Sanders: Graphs 1 Mathematical View: Just Sets and Pairs INFORMATIK Often terminology is already reason enough for a graph model 1.1 Directed Graphs ´Î µ describes a directed graph with vertex set Î and edge set ¢ . Î Î Convention: and Ñ Ò Î 1.1.1 Special Cases (usually disallowed) Multigraphs: parallel edges allowed, i.e, is a multiset. self loops: edges ´Ú Úµ
- 3. Sanders: Graphs 3 1.1.2 Degrees INFORMATIK ¾ ´Ú out-degree´Úµ: Ùµ ¾ ´Ù in-degree´Úµ: Úµ
- 4. Sanders: Graphs ¾ ¾ ´Ù ´Ú INFORMATIK Bidirected graph: Úµ Ùµ 1.2 Undirected Graphs streamlined description of a bidirected graph where edges are two element vertex sets. ° ´Ù ´Ú ÙÚ Úµ Ùµ degree´Úµ in-degree´Úµ out-degree´Úµ What about self loops?
- 5. Sanders: Graphs 5 1.3 Subgraphs INFORMATIK ¼ ´Î ¼ ¼ µ is subgraph of µ if Î ¼ ¼¾ ´Î and Î ¼ is vertex induced by Î ¼ if ¼ ¼ ¼ ¾ ¾ ¾ ´Ù Úµ Ù Î Ú Î
- 6. Sanders: Graphs 1.4 Associated Information INFORMATIK Ê edge weights Û Example: Length or trafﬁc capacity of a street, we will see more things like node potentials, node/vertex colors,
- 7. Sanders: Graphs 7 1.5 Paths INFORMATIK A path Ô Ú¼ Ú of length connects nodes Ú¼ and Ú if subsequent nodes in Ô are connected by edges in , i.e, ¾ ¾ ¾ ½ ´Ú¼ Ú½ µ ´Ú½ Ú¾ µ ´Ú µ ,¾ , , . ½ Ú edge representation: . ½ Ô simple path: Ú¼ , ,Ú different. Nonsimple paths are usually called walks. È µ. In weighted graphs, length is ½ Û´ bottleneck weight: Ñ Ò µ. ½ Û´ Applications: What is the shortest route from Saarbrucken to Paris? ¨ How much trafﬁc can it carry?
- 8. Sanders: Graphs 1.6 Cycles INFORMATIK Cycles are paths that share the ﬁrst and the last node A cycle is simple if all other nodes are different. Hamiltonian cycle: simple cycle that visits all nodes. Euler cycle: cycle (cyclic walk) that visits all edges once back to the bridge problem. But we need one more term.
- 9. Sanders: Graphs 9 1.7 Connectedness INFORMATIK ¸ ¾ is strongly connected path Ô connects Ù and Ú ÙÚ Î Ô drop the “strongly” for undirected graphs A connected component is a maximal connected vertex induced subgraph
- 10. Sanders: Graphs 1.8 Euler Tours INFORMATIK Theorem 1. is Eulerian (has an Euler tour) if and only if is connected and every node has even degree. Proof: Necessity: connectedness is clear. ½. Consider a node Ú with degree ¾ The tour is stuck when it visits Ú the -th time. Sufﬁciency: Step 1, Construct Euler Paritioning of into a set of cycles: Greedily build any walk. Remove visited edges. When you get stuck, you have closed a cycle. Step 2, Glue the cycles together. Possible since any cycle intersects some other cycle.
- 11. Sanders: Graphs 11 1.9 Trees INFORMATIK An undirected graph is a tree if there is exactly one path between any pair of nodes. Exercise: Prove that the following properties are equivalent 1. is a tree. ½ edges. 2. is connected and has exactly Ò 3. is connected and contains no cycles. Forest: undirected acyclic graph Directed acyclic graph (DAG): no cycles. Exercise: How many edges are possible?
- 12. Sanders: Graphs 1.10 Directed Trees? INFORMATIK we need a special root node Ö. ¾ exactly one Ö path or Ú Î Ú ¾ exactly one Ú path Ú Î Ö CS trees have the root at the top. a parent is immediately above its children or successors which are siblings. (ancestors, ) Nodes without children are leaves. Nonroot nonleaf nodes are interior. ordered trees: order of children matters. Example: search trees.
- 13. Sanders: Graphs 13 2 Graph Representation INFORMATIK we mostly represent undirected graphs as bidirected graphs focus on directed graphs Overview: ¾ Operations are what matters ¾ A trivial representation ¾ Arrays ¾ Linked Lists ¾ Matrices ¾ Implicit Representation ¾ Discussion
- 14. Sanders: Graphs 2.1 Operations INFORMATIK Ç´½µ time for all elementary operations Goal: Access associated information. Use arrays. Perhaps hash tables. ½ One reason for Î Ò. Navigation: Given Ú ﬁnd outgoing edges. (We may also want incoming edges.) ¾ Edge queries: ´Ù ? Adjanceny matrices, hash tables. Úµ Reverse access: Given ´Ù ﬁnd ´Ú Úµ Ùµ Construction conversion and output (Ç´Ñ · Òµ time) Update: Insert/delete nodes/edges. This is the hard part.
- 15. Sanders: Graphs 15 Example: Recognizing DAGs INFORMATIK ¾ ¼ do delete Ú while out-degree´Úµ Ú Î if Î then output “ is a DAG” else output “ contains a cycle” // to ﬁnd a cycle, pick any node and follow edges works because we never destroy or introduce cycles Ç´Ñ · Exercise: implement it in time Òµ
- 16. Sanders: Graphs 2.2 Edge Sequence Representation INFORMATIK Sequence of node pairs (or triples with edge weight) · Compact · Good for I/O Almost no useful operations except scanning all edges Not so bad with some additional node arrays. Examples: Find isolated nodes, several MST algorithms, conversion.
- 17. Sanders: Graphs 17 2.3 Adjacency Arrays INFORMATIK ½ ¼ Î Ò Edge array E stores targets grouping edges leaving a node Node array V stores index of ﬁrst outgoing edge stores Ñ · ½ dummy entry Î Ò 1 0 n−1 4=n V0 2 466 0 3 E1 2 2 3 1 3 m−1 6=m 0 2 Î ÎÚ·½ Example: out-degree´Úµ Ú
- 18. Sanders: Graphs 2.4 Edge List Adjacency Array INFORMATIK A Reminder: Sorting Small Integers // make a sorted permutation of Procedure KSortArray´a,b : Array ½ of 0..K-1µ Ò ½ of Æ ¼ ¼ : Array ¼ c // counters for each bucket Ã for := ½ to Ò do ·· // Count bucket sizes C := 0 È ½ do ´ := ¼ to Ã µ := ´ · µ// Store for in for := ½ to Ò do // Distribute := ·· Idea: Sort by starting node
- 19. Sanders: Graphs 19 Integrated Solution INFORMATIK Ä ×Ø µ Function adjacencyArray´ of Æ ¼ ¼ : Array ¼ V Ò ¾ EdgeList do foreach ´Ù ·· // count Úµ ÎÙ ½ for Ú := ½ to Ò do +=Î // preﬁx sums ÎÚ Ú ¾ EdgeList do foreach ´Ù // place Úµ ÎÙ Ú return ´Î µ Example on Blackboard
- 20. Sanders: Graphs Operations for Adjacency Arrays INFORMATIK Navigation: easy. we will see several applications Edge weights: becomes array of records Incoming Edges: another (reverse) edge array Delete Edges: explicit end indices Batched Updates: rebuild
- 21. Sanders: Graphs 21 2.5 Adjacency Lists INFORMATIK store (doubly linked) list of adjacent edges for each node (a bit more restricted with singly linked lists) · easy edge insertion · easy edge deletion (order preserving) more space (factor up to 3) than adj. arrays more cache faults than adj. arrays
- 22. Sanders: Graphs Enhancing Adjacency Lists INFORMATIK For reverse edge access or edge weight updates in undirected graphs: explicit edge objects ¾ stores weights ¾ pred/succ-pointers for all adjacency lists containing this edge ¾ reverse pointer for directed graphs ¾ cute trick: store only xor of incident nodes
- 23. Sanders: Graphs 23 Node insertion/deletion INFORMATIK mark as deleted swap in node Ò list of nodes µ node handles are pointers. 1 0 3 Problem: graphs with common node set E list out list in list rev from to 2 (0,1) 0 0 0 V list first first deg deg out in out in (0,2) 0 0 0 0 0 2 0 (1,2) 1 0 2 2 (1,3) 2 0 00 2 2 (2,1) 3 0 0 0 0 0 2 (2,3) 0 0 0 0 ¢ more space than vanilla adjacency arrays
- 24. Sanders: Graphs 2.6 Customization INFORMATIK Customize data structure for application for maximum speed/compactness. Sofware Engineering nightmare Seperating algorithm from their representation may be a way out
- 25. Sanders: Graphs 25 Example: Recognizing dags INFORMATIK ¾ ¼ do delete Ú while out-degree´Úµ Ú Î ¾ Use adjacency array storing incoming edges ¾ Store out-degrees ¾ Maintain a stack of out-degree zero nodes ¾ delete ´Ù by decrementing out-degree´Ùµ Úµ ¾ no explicit node deletion ¾ At the end test whether all nodes have out-degree´¼µ
- 26. Sanders: Graphs 2.7 Adjacency Matrix INFORMATIK Ò¢Ò with ¾ ¾ ¼½ ´ µ ´ µ · space efﬁcient for very dense matrices space inefﬁcient otherwise. Exercise: what should “very dense” mean? · Easy edge queries Slow navigation (except for dense graphs) ·· joins linear algebra and graph theory # -edge paths from to Example: . Exercise: count -edge paths Important accelerators: Ç´ÐÓ µ mat-mults for mat-power matrix multiplication in subcubic time, e.g., Strassen’s algorithm
- 27. Sanders: Graphs 27 Example where graph theory helps LA INFORMATIK Ü ´½ ´ µ ¼µ Problem: solve Consider Ò µ Assume has two connected components we swap rows and cols such that ¼ ½¼ ½ ¼ ½ ¼ Ü½ ½ ½ ¼ Ü¾ ¾ ¾ Exercise: What if is a DAG?
- 28. Sanders: Graphs 2.8 Implicit Representation INFORMATIK Compact representations of possibly very dense graphs Implement algorithms directly using this representation
- 29. Sanders: Graphs 29 Example: Connectedness of Interval Graphs INFORMATIK ÒÒ ½ ½ Î and overlap Idea: sweep intervals from left to right. The number of overlapping intervals may never drop to zero. Function isConnected(Ä : SortedListOfIntervalEndPoints) : ¼ ½ remove ﬁrst element of Ä overlap := 1 ¾ foreach Ô do Ä ¼ return 0 if overlap if Ô is a start point then overlap·· else overlap // end point ¾¡ Ç´Ò ÐÓ Ç algorithm for up to edges! Exercise: ﬁnd all connected Òµ Ò components