CS 4407 Algorithms Lecture 2: Graphs Reviegprovan/CS4407/2016/L2-Graphs.pdf · CS 4407 Algorithms...
Transcript of CS 4407 Algorithms Lecture 2: Graphs Reviegprovan/CS4407/2016/L2-Graphs.pdf · CS 4407 Algorithms...
1
CS 4407
Algorithms
Lecture 2:
Graphs—Review
Prof. Gregory Provan
Department of Computer Science
University College Cork
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Outline
Motivation
– Importance of graphs for algorithm design
– applications
Overview of algorithms to study
Review of basic graph theory
CS 4407, Algorithms University College Cork,
Gregory M. Provan
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 University College Cork,
Gregory M. Provan
Motivation
For theoreticians:
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 University College Cork,
Gregory M. Provan
Examples of Networks
communication
Network
telephone exchanges,
computers, satellites
Nodes Arcs
cables, fiber optics,
microwave relays
Flow
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
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Graph Algorithms Overview
Standard graph algorithms
– Breadth-first search (BFS), Depth-first search
(DFS), heuristic algorithms
– Minimum Spanning Tree
– Shortest Path
– Max-Flow
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Motivations for Definition
Algorithm/Concept Definition BFS, DFS Graph, adjacency structure,
directionality
MST Trees, directionality, Weighted graph
Network Flows Weighted graph
Greedy algorithms Graph, adjacency structure,
directionality
MapReduce Graph, adjacency structure,
directionality
NP-complete: Hamilton cycle Hamilton cycle, paths, cycles
TSP Paths, cycles
graph isomorphism Graph isomorphism
CS 4407, Algorithms University College Cork,
Gregory M. Provan
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
CS 4407, Algorithms University College Cork,
Gregory M. Provan
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.
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Examples of Digraphs & Graphs
Figure B.2
CS 4407, Algorithms University College Cork,
Gregory M. Provan
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).
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Combinatorial Facts
In a graph
• 0 |E | C(| V |, 2) = | V | (| V | – 1) / 2 O(| V | 2)
• vV degree(v) = 2 | E |
In a digraph
• 0 | E | | V | 2
• vV in-degree(v) = vV out-degree(v) = | E |
A graph is said to be sparse if | E | O(| V |), and dense
otherwise.
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Definitions (Path vs. Cycle)
Path: a sequence of vertices <v0, …, vk> such that (vi-
1, vi) 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 v0 = vk. 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.
CS 4407, Algorithms University College Cork,
Gregory M. Provan
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.
CS 4407, Algorithms University College Cork,
Gregory M. Provan
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, vE iff ( f(u), f(v)) E’.
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Examples of Isomorphic Graphs
Figure B.3
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Graphs, Trees, Forests
Free Tree Forest DAG Trees
CS 4407, Algorithms University College Cork,
Gregory M. Provan
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.
CS 4407, Algorithms University College Cork,
Gregory M. Provan
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 University College Cork,
Gregory M. Provan
Graphs: Adjacency Matrix
Example:
1
2 4
3
a
d
b c
A 1 2 3 4
1
2
3 ?? 4
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Graphs: Adjacency Matrix
Example:
1
2 4
3
a
d
b 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
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Graphs: Adjacency Matrix
How much storage does the adjacency matrix
require?
A: O(V2)
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 University College Cork,
Gregory M. Provan
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 University College Cork,
Gregory M. Provan
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 University College Cork,
Gregory M. Provan
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
CS 4407, Algorithms University College Cork,
Gregory M. Provan
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
bij = { -1, if edge j leaves vertex i }
bij = { 1, if edge j enters vertex i }
bij = { 0, otherwise } – Note: must have no self-loops.
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Example for Graphs
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).
Figure 22.1
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Example for Digraphs
Figure 22.2
CS 4407, Algorithms University College Cork,
Gregory M. Provan
Lecture Summary
Motivation for studying graphs
– Importance of graphs for algorithm design
– applications
Overview of algorithms
– Basic algorithms: DFS, BFS
– More advanced algorithms: flows, tree-decompositions
Review of basic graph theory