CS 4407 Algorithms Lecture 5: Graphs an Introduction Prof. Gregory Provan Department of Computer Science University College Cork 1
Outline Motivation Importance of graphs for algorithm design applications Overview of algorithms to study Review of basic graph theory
Today s Learning Objectives Why graphs are useful throughout Computer Science Range of applications is large We use some basic properties of graphs CS 4407, Algorithms
For theoreticians: Motivation Graph problems are neat, often difficult, hence interesting For practitioners: Massive graphs arise in networking, web modelling,... Problems in computational geometry can be expressed as graph problems Many abstract problems best viewed as graph problems Extreme: Pointer-based data structures = graphs with extra information at their nodes CS 4407, Algorithms
Examples of Networks Network Nodes Arcs Flow communication telephone exchanges, computers, satellites cables, fiber optics, microwave relays voice, video, packets circuits gates, registers, processors wires current mechanical joints rods, beams, springs heat, energy hydraulic reservoirs, pumping stations, lakes pipelines fluid, oil financial stocks, currency transactions money transportation airports, rail yards, street intersections highways, railbeds, airway routes freight, vehicles, passengers chemical sites bonds energy
Graph Algorithms Overview Standard graph algorithms Breadth-first search (BFS), Depth-first search (DFS), heuristic algorithms Minimum Spanning Tree Shortest Path Max-Flow Tree-Decomposition Algorithms Convert arbitrary graphs to trees of cliques CS 4407, Algorithms
Applications Networking Internet, communication, transportation VLSI & logic circuit design Graphics surface meshes in CAD/CAM AI and Robotics applications path planning for autonomous agents precedence constraints in scheduling
Motivations for Definition Algorithm/Concept BFS, DFS MST Network Flows Greedy algorithms MapReduce NP-complete: Hamilton cycle TSP graph isomorphism Definition Graph, adjacency structure, directionality Trees, directionality, Weighted graph Weighted graph Graph, adjacency structure, directionality Graph, adjacency structure, directionality Hamilton cycle, paths, cycles Paths, cycles Graph isomorphism
Graphs A collection of vertices or nodes, connected by a collection of edges. Useful in many applications where there is some connection or relationship or interaction between pairs of objects. network communication & transportation VLSI design & logic circuit design surface meshes in CAD/CAM path planning for autonomous agents precedence constraints in scheduling
Basic Definitions A directed graph (or digraph) G = (V, E) consists of a finite set V, called vertices or nodes, and E, a finite set of ordered pairs, called edges of G. E is a binary relation on V. Cycles, including self-loops are allowed. Multiple edges are not allowed though; (v, w) and (w, v) are distinct edges. An undirected graph (or simply a graph) G = (V, E) consists of a finite set V of vertices, and a finite set E of unordered pairs of distinct vertices, called edges of G. Cycles are allowed, but not self-loops. Multiple edges are not allowed.
Examples of Digraphs & Graphs Figure B.2
Definitions Vertex v is adjacent to vertex u if there is an edge (u, v). Given an edge e = (u, v) in an undirected graph, u and v are the endpoints of e, and e is incident on u and on v. In a digraph with edge e = (u, v), u and v are the origin and destination. We say that e leaves u and enters v. A digraph or graph is weighted if its edges are labeled with numeric values. In a digraph, the Out-degree of v is the number of edges coming from v. the In-degree of v is the number of edges coming into v. In a graph, the degree of v is the number of edges incident to v. (The in-degree equals the out-degree).
Combinatorial Facts In a graph 0 E C( V, 2) = V ( V 1) / 2 O( V 2 ) v V degree(v) = 2 E In a digraph 0 E V 2 v V in-degree(v) = v V out-degree(v) = E A graph is said to be sparse if E O( V ), and dense otherwise. CS 4407, Algorithms
Definitions (Path vs. Cycle) Path: a sequence of vertices <v 0,, v k > such that (v i- 1, v i ) is an edge for i = 1 to k, in a digraph. The length of the path is the number of edges, k. w is reachable from u if there is a path from u to w. A path is simple if all vertices are distinct. Cycle: a path in a digraph containing at least one edge and for which v 0 = v k. A cycle is simple if, in addition, all vertices are distinct. For graphs, the definitions are the same, but a simple cycle must visit 3 distinct vertices.
Historical Terms For Cycles and Paths An Eulerian cycle is a cycle, not necessarily simple, that visits every edge of a graph exactly once. A Hamiltonian cycle (or path) is a cycle (path in a directed graph) that visits every vertex exactly once.
Definitions (Connectivity) A graph is acyclic, if it contains no simple cycles. A graph is connected, if every one of its vertices can reach every other vertex. I.e., every pair of vertices is connected by a path. The connected components of a graph are equivalence classes of vertices under the is reachable from relation. A digraph is strongly connected, if every two vertices are reachable from each other. Graphs G = (V, E) and G = (V, E ) are isomorphic, if a bijection f : V V such that u, v E iff ( f(u), f(v)) E.
Examples of Isomorphic Graphs Figure B.3
Graphs, Trees, Forests Free Tree Forest DAG Trees
DAGs versus Trees A tree is a digraph with a non-empty set of nodes such that: There is exactly one node, the root, with in-degree of 0. Every node other than the root has in-degree 1. For every node a of the tree, there is a directed path from the root to a. Textbook (CLRS) suggests that a tree is an undirected graph, by association with free trees. This is a valid approach, if you accept that the existence of the distinguished vertex (root) induces a direction on all the edges of the graph. However, we usually think of trees as being DAGs. Notice that a DAG may not be a tree, even if a root is designated.
Representing Graphs Assume V = {1, 2,, n} An adjacency matrix represents the graph as a n x n matrix A: A[i, j] = 1 if edge (i, j) E (or weight of edge) = 0 if edge (i, j) E CS 4407, Algorithms
Example: Graphs: Adjacency Matrix a 1 2 4 b 3 d c A 1 2 3 4 1 2 3?? 4
Example: Graphs: Adjacency Matrix a 1 2 4 b 3 d c A 1 2 3 4 1 0 1 1 0 2 0 0 1 0 3 0 0 0 0 4 0 0 1 0
Graphs: Adjacency Matrix How much storage does the adjacency matrix require? A: O(V 2 ) What is the minimum amount of storage needed by an adjacency matrix representation of an undirected graph with 4 vertices? A: 6 bits Undirected graph matrix is symmetric No self-loops don t need diagonal CS 4407, Algorithms
Graphs: Adjacency Matrix The adjacency matrix is a dense representation Usually too much storage for large graphs But can be very efficient for small graphs Most large interesting graphs are sparse E.g., planar graphs, in which no edges cross, have E = O( V ) by Euler s formula For this reason the adjacency list is often a more appropriate representation CS 4407, Algorithms
Graphs: Adjacency List Adjacency list: for each vertex v V, store a list of vertices adjacent to v Example: Adj[1] = {2,3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3} Variation: can also keep a list of edges coming into vertex 1 2 4 3 CS 4407, Algorithms
Graphs: Adjacency List How much storage is required? The degree of a vertex v = # incident edges Directed graphs have in-degree, out-degree For directed graphs, # of items in adjacency lists is out-degree(v) = E takes (V + E) storage (Why?) For undirected graphs, # items in adj lists is degree(v) = 2 E (handshaking lemma) also (V + E) storage So: Adjacency lists take O(V+E) storage
Graph Representations Let G = (V, E) be a digraph. Adjacency Matrix: a V V matrix for 1 v,w V A[v, w] = 1, if (v, w) E and 0 otherwise If digraph has weights, store them in the matrix. Adjacency List: an array Adj[1 V ] of pointers where for 1 v V, Adj[v] points to a linked list containing the vertices adjacent to v. If the edges have weights then they may also be stored in the linked list elements. Incidence Matrix: a V E matrix, B[i, j], of elements b ij = { -1, if edge j leaves vertex i } b ij = { 1, if edge j enters vertex i } b ij = { 0, otherwise } Note: must have no self-loops.
Example for Graphs Figure 22.1 NOTE: it is common to include cross links between corresponding edges, when needed to mark the edges previously visited. E.g. (v,w) = (w,v). CS 4407, Algorithms
Example for Digraphs Figure 22.2
Lecture Summary Motivation for studying graphs Importance of graphs for algorithm design applications Overview of algorithms Basic algorithms: DFS, BFS More advanced algorithms: flows, treedecompositions Review of basic graph theory