Connectivity information Graphs

Connectivity information is present, e.g., in city maps, where the objects are roads, and Graph is a set of objects, called vertices, also in the routing tables for the Internet, together with a collection of pair-wise when the objects are computers. connections, called edges, between them Connectivity information is also present in Graphs have applications in a number of the parent child relationships defined by a different domains, including mapping (in binary tree, where the objects are tree geographic information systems), nodes. transportation (in road and flight networks), electrical engineering (in Connectivity information can be defined by circuits), and computer networking (in the all kinds of relationships that exist between connections of the Internet) pairs of objects.

week 6 Complexity of Algorithms 1 week 6 Complexity of Algorithms 2

Graphs Graph (example)

Formally graph G=(V,E) is a set V of Graph of co-authorship vertices and a collection E of pairs of vertices from V, called edges. Edges in graph are either directed or undirected An edge (u,v) is said to be directed from u to v if the pair (u,v) is directed An edge (u,v) is said to be undirected if the pair (u,v) is not ordered

week 6 Complexity of Algorithms 3 week 6 Complexity of Algorithms 4

1 Graphs Graph (example)

Two vertices are said to be adjacent if they are end-points of the same edge. An edge is said to be incident on a if the vertex is one of the edge end-points An outgoing edge of a vertex is a directed edge whose origin is in that vertex An incoming edge of a vertex is a directed edge whose destination is that vertex The degree of a vertex v, denoted by deg(v), is the number edges on v The in-degree (out-degree) of a vertex v is the number of incoming (outgoing) of v, and it is denoted by in-deg(v) (out-deg(v)) week 6 Complexity of Algorithms 5 week 6 Complexity of Algorithms 6

Graphs Graphs

Thm: If G is a graph with m edges, Thm: Let G be a simple graph with n then vertices and m edges. If G is undirected, then m £ n(n-1)/2, and If G is directed, then m £ n(n-1) A path in a graph is a sequence of Thm: If G is a with m alternating vertices and edges that starts edges, then at a vertex and ends at a vertex. A cycle is a path with the same start and end vertices

week 6 Complexity of Algorithms 7 week 6 Complexity of Algorithms 8

2 Graphs Graphs

We say that the path is simple if each A sub-graph of a graph G is a graph H vertex in the path is distinct whose vertices and edges are sub-sets of the vertices and edges of G We also say that the cycle is simple if each vertex in the cycle is distinct, except for A spanning sub-graph of G is a sub-graph of G that contains all the vertices of graph G the first and the last one A graph is connected if, for any two A directed path (directed cycle) is a path vertices, there is a path between them (cycle), s.t., all the edges are directed and If a graph is not connected, its maximal are traversed along their direction connected sub-graphs are called the connected components of G

week 6 Complexity of Algorithms 9 week 6 Complexity of Algorithms 10

Graphs Graphs

A forest is a graph without cycles Let G be an undirected graph with n A tree is a connected forest, i.e., vertices and m edges. Then we have connected graph without cycles the following: A tree with a distinguished node (a root) is If G is connected, then m ³ n-1 called a rooted tree, otherwise it is If G is a tree, then m = n-1 referred as a free tree If G is a forest, then m £ n-1 A spanning tree of a graph is a spanning sub-graph that is a (free) tree

week 6 Complexity of Algorithms 11 week 6 Complexity of Algorithms 12

3 Adjacency List Adjacency Matrix

week 6 Complexity of Algorithms 13 week 6 Complexity of Algorithms 14

Depth-First Search (DFS) DFS (pseudo-code)

A

B C

D F E G H I J DFS tree edges week 6 Complexity of Algorithms 15 week 6 Complexity of Algorithms 16

4 DFS (applications) Biconnected Graph

Let G be a connected graph A separation edge of G is an edge whose removal disconnects G A separation vertex of G is a vertex whose removal disconnects G A connected graph G is biconnected if, for any two vertices u and v of G, there are two disjoint paths (composed of different edges) between u and v

week 6 Complexity of Algorithms 17 week 6 Complexity of Algorithms 18

Biconnected Graph Biconnected Components

Let G be a connected graph. The following are equivalent: G is biconnected For any two vertices of G, there is a simple cycle containing them G does not have separation vertices or separation edges

week 6 Complexity of Algorithms 19 week 6 Complexity of Algorithms 20

5 Finding Biconnected Components Finding Biconnected Components

We are looking for unions Thm: Given a connected graph G with of edge intersecting cycles m edges. We can compute G’s biconnected components, separation vertices, and separation edges in O(m) time.

week 6 Complexity of Algorithms 21 week 6 Complexity of Algorithms 22

Breadth-First Search (BFS) BFS (pseudo-code)

A

B C

D F E G H I J

week 6 Complexity of Algorithms 23 week 6 Complexity of Algorithms 24

6 BFS (applications) DFS/BFS Comparison

DFS traversal: is better for answering complex connectivity questions, Produces a spanning tree, s.t., all non-tree edges are back edges BFS traversal: Is better in finding shortest paths in a graph, Produces a spanning tree, s.t., all non-tree edges are cross edges

week 6 Complexity of Algorithms 25 week 6 Complexity of Algorithms 26

Digraphs Digraphs

A digraph is a graph whose edges are all A digraph G is strongly connected if, directed for any two vertices u and v of G, u A fundamental issue with directed graphs is reaches v and v reaches u the notion of reachability, which deals with A directed cycle of G is a cycle where determining where we can get to in a all the edges are traversed according directed graph. to their respective directions Given two vertices u and v of a digraph G, A digraph is acyclic if it has no we say that u reaches v (v is reachable directed cycles from u) if G has a directed path from u to v

week 6 Complexity of Algorithms 27 week 6 Complexity of Algorithms 28

7 Testing for Strong Connectivity Testing for Strong Connectivity

v v Build a directed DFS tree T(v) in G G rooted in arbitrary vertex v Gr

Construct graph Gr by changing directions of all edges in G

Build a directed DFS tree Tr(v) in Gr

If trees T(v) and Tr(v) are spanning trees then G is strongly connected. Complexity: O(n+m)

week 6 Complexity of Algorithms 29 week 6 Complexity of Algorithms 30

8