Algorithm Design and Analysis (ADA)
description
Transcript of Algorithm Design and Analysis (ADA)
242-535 ADA: 8. Intro. Graphs
1
• Objectiveo introduce the main kinds of graphs, discuss
two implementation approaches, and remind you about trees
Algorithm Design and Analysis (ADA)
242-535, Semester 1 2016-2017
8. Introduction
to Graphs
242-535 ADA: 8. Intro. Graphs
2
1. Graphs2. Graph Terminology3. Implementing Graphs
- adjency matrix- adjency list
4. Trees and Forests5. Tree Terminology
Overview
1. Graphs• A graph has two parts (V, E), where:
o V are the nodes, called verticeso E are the links between vertices, called edges
• Example:o airports and distance between them
ORD PVD
MIADFW
SFO
LAX
LGA
HNL
849
802
13871743
1843
109911201233
3372555
142
242-535 ADA: 8. Intro. Graphs
4
• Directed grapho the edges are directedo e.g., bus cost network
• Undirected grapho the edges are undirectedo e.g., road network
Graph Types
242-535 ADA: 8. Intro. Graphs
5
Example Nodes EdgesTransportation network: airline routes
airports nonstop flights
Communication networks
computers, hubs, routers
physical wires
Information network: web
pages hyperlinks
Information network: scientific papers
articles references
Social networks people “u is v’s friend”, “u sends email to v”, “u’s FaceBook links to v”
Computer programs functions (or modules)statement blocks
“u calls v”
“v can follow u”
Graphs are everywhere
242-535 ADA: 8. Intro. Graphs
6
• A calling graph for a program:
A Calling Graph
main
printList
mergeSort
mergesplit
makeList
4 examples ofrecursion
242-535 ADA: 8. Intro. Graphs
7
• Problem: minimise the moving time of the drill over a metal sheet.
Sheet Metal Hole Drilling
continued
242-535 ADA: 8. Intro. Graphs
8
• Add edge numbers (weights) for the movement time between any two holes.
A Weighted Graph
129
4
4
35
6
86
a
d
e
c
b
2
242-535 ADA: 8. Intro. Graphs
9
• End vertices (or endpoints) of an edgeo U and V are the endpoints
• Edges incident on a vertexo a, d, and b are incident
• Adjacent verticeso U and V are adjacent
• Degree of a vertexo X has degree 5
• Parallel edgeso h and i are parallel edges
• Self-loopo j is a self-loop
2. Graph Terminology
XU
V
W
Z
Y
a
c
b
ed
fg
h
i
j
242-535 ADA: 8. Intro. Graphs
10
• Patho sequence of alternating
vertices and edges o begins with a vertexo ends with a vertexo each edge is preceded and
followed by its endpoints
• Simple patho path such that all its vertices
and edges are distinct
• Exampleso P1=(V,b,X,h,Z) is a simple
patho P2=(U,c,W,e,X,g,Y,f,W,d,V)
is a path that is not simple
P1
XU
V
W
Z
Y
a
c
b
e
d
fg
hP2
242-535 ADA: 8. Intro. Graphs
11
• Cycleo circular sequence of
alternating vertices and edges
o each edge is preceded and followed by its endpoints
• Simple cycleo cycle such that all its vertices
and edges are distinct
• Exampleso C1=(V,b,X,g,Y,f,W,c,U,a) is
a simple cycleo C2=(U,c,W,e,X,g,Y,f,W,d,V,a
,) is a cycle that is not simpleGraphs 11
C1
XU
V
W
Z
Y
a
c
b
e
d
fg
hC2
Connectivity• A graph is connected if
there is a path between every pair of vertices
Connected graph
Non connected graph with two connected components
Strong Connectivity• Each vertex can reach all other
vertices
13
a
d
c
b
e
f
g
242-535 ADA: 8. Intro. Graphs
14
• All pairs of vertices are connected by an edge.• No. of edges |E| = |V| (|V-1|)/2 = O(|V|2)
Complete Graphs
242-535 ADA: 8. Intro. Graphs
15
• Directed Acyclic Grapho no cycle
• Eulerian Grapho must visit each edge once
• Bipartiteo 2 sets, no edges within set!
Some Common Graphs
242-535 ADA: 8. Intro. Graphs
16
• A DAG has no cycles• Some algorithms become simpler when used
on DAGs instead of general graphs, based on the principle of topological orderingo find shortest paths and longest paths by
processing the vertices in a topological order
Directed Acyclic Graph (DAG)
242-535 ADA: 8. Intro. Graphs
17
• An Euler graph has either an Euler path or Euler tour.
• An Euler path is defined as a path in a graph which visits each edge exactly once.
• An Euler tour/cycle is an Euler path which starts and ends on the same vertex.
Euler Graph
242-535 ADA: 8. Intro. Graphs
18
• A graph whose vertices can be divided into two disjoint sets U and Vo every edge connects a vertex in U to one in V
Bipartite graph (bigraph)
242-535 ADA: 8. Intro. Graphs
19
• Search/traversalo visit every node once (Euler)o visit every edge once (Hamiltonian)
• Shortest paths• Minimal Spanning Trees (MSTs)• Network flow
• Matching• Graph coloring• Travelling salesman problem
Graph-related Problems
242-535 ADA: 8. Intro. Graphs
20
• We will typically express running times in terms of |E| and |V| (often dropping the |’s)o If |E| |V|2 the graph is dense• can also write this as |E| is O(|v2|)
o If |E| |V| the graph is sparse• or |E| is O(|V|)
• Dense and sparse graphs are best implemented using two different data structures:o Adjacency matricies: for dense graphso Adjacency lists: for sparse graphs
3. Implementing Graphs
242-535 ADA: 8. Intro. Graphs
21
• In the most dense graph, a graph of v verticies will have |V|(|V|-1)/2 edges.
• In that case, for large n, |E| is O(|V|2)
Dense Big-Oh
|V| = 5|E| = (5*4)/2 = 10
242-535 ADA: 8. Intro. Graphs
22
3.1. Adjacency Matrix
a b
c
ed
Graph
0 1 0 0 1a b c d e
1 0 1 0 10 1 1 0 10 0 0 0 11 1 1 1 0
abc
de
Adjacency Matrix
242-535 ADA: 8. Intro. Graphs
23
• An adjacency matrix represents the graph as a V * V matrix A:o A[i, j] = 1 if edge (i, j) E
= 0 if edge (i, j) E
• The degree of a vertex v (of a simple graph) = sum of row v or sum of column vo e.g. vertex a has degree 2 since it is connected to b
and e
• An adjacency matrix can represent loopso e.g. vertex c on the previous slide
Properties
continued
242-535 ADA: 8. Intro. Graphs
24
• An adjacency matrix can represent parallel edges if non-negative integers are allowed as matrix entrieso ijth entry = no. of edges between vertex i and j
• The matrix duplicates information around the main diagonalo the size can be easily reduced with some coding
tricks
• Properties of graphs can be obtained using matrix operationso e.g. the no. of paths of a given length, and vertex
degree
242-535 ADA: 8. Intro. Graphs
25
• If an adjacency matrix A is multiplied by itself repeatedly:o A, A2, A3, ..., An
Then the ijth entry in matrix An is equal to the number of paths from i to j of length n.
The No. of Paths of Length n
242-535 ADA: 8. Intro. Graphs
26
Example
a b
c
ed
0 1 0 1 0a b c d e
1 0 1 0 10 1 0 1 11 0 1 0 00 1 1 0 0
abc
de
A =
242-535 ADA: 8. Intro. Graphs
27
0 1 0 1 01 0 1 0 10 1 0 1 11 0 1 0 00 1 1 0 0
A2 =
0 1 0 1 01 0 1 0 10 1 0 1 11 0 1 0 00 1 1 0 0
=
2 0 2 0 1
a b c d e
0 3 1 2 12 1 3 0 10 2 0 2 11 1 1 1 2
a
bc
d
e
242-535 ADA: 8. Intro. Graphs
28
• Consider row a, column c in A2:
Why it Works...
( 0 1 0 1 0 )a
b d
c
0
1
01
1
b
d= 0*0 + 1*1 + 0*0 + 1*1 + 0*1= 2
continued
a-b-c a-d-c
242-535 ADA: 8. Intro. Graphs
29
• A non-zero product means there is at least one vertex connecting verticies a and c.
• The sum is 2 because of:o (a, b, c) and (a, d, c)o 2 paths of length two
242-535 ADA: 8. Intro. Graphs
30
• The entries on the main diagonal of A2 give the degrees of the verticies (when A is a simple graph).
• Consider vertex c:o degree of c == 3 since it is connected to the edges
(c,b), (c,d), and (c,e).
The Degree of Verticies
continued
242-535 ADA: 8. Intro. Graphs
31
• In A2 these become paths of length 2:o (c,b,c), (c,d,c), and (c,e,c)
• So the number of paths of length 2 for c = the degree of co this is true for all verticies
242-535 ADA: 8. Intro. Graphs
32
• #define NUMNODES nint arcs[NUMNODES][NUMNODES];
• arcs[u][v] == 1 if there is an edge (u,v); 0 otherwise
• Storage used: O(|V|2)
• The implementation may also need a way to map node names (strings) to array indicies.
Coding Adjacency Matricies
continued
242-535 ADA: 8. Intro. Graphs
33
• If n is large then the array will be very large, with almost half of it being unnecessary.
• If the nodes are lightly connected then most of the array will contain 0’s, which is a further waste of memory.
242-535 ADA: 8. Intro. Graphs
34
• A directed graph:
Representing Directed Graphs
0 1
32
4
242-535 ADA: 8. Intro. Graphs
35
• Not symmetric; all the array may be necessary.
• Still a waste of space if nodes are lightly connected.
Its Adjacency Matrix
1 1 1 0 00 0 0 1 01 1 0 0 10 0 1 0 10 1 0 0 0
00 1 2 3 4
1234
finish
star
t
242-535 ADA: 8. Intro. Graphs
36
• The adjacency matrix is an efficient way to store dense graphs.
• But most large interesting graphs are sparseo e.g., planar graphs, in which no edges cross, have
|e| = O(|v|) by Euler’s formula
o For this reason the adjacency list is often a better respresentation than the adjacency matrix
When to use an Adjacency Matrix
242-535 ADA: 8. Intro. Graphs
37
• Adjacency list: for each vertex v V, store a list of vertices adjacent to v
• Example:o adj[0] = {0, 1, 2}o adj[1] = {3}o adj[2] = {0, 1, 4}o adj[3] = {2, 4}o adj[4] = {1}
• Can be used for directed and undirected graphs.
3.2. Adjacency List
0 1
32
4
242-535 ADA: 8. Intro. Graphs
38
• An implementation diagram:
0 1 2
3
0 1 4
2 4
1
0
1
2
3
4
adj[]
meansNULL
size of array= no. ofvertices (|V|) no. of cells
== no. of edges (|E|)
242-535 ADA: 8. Intro. Graphs
39
• struct cell { /* for a linked list */ Node nodeName; struct cell *next;};
struct cell *adj[NUMNODES];
• adj[u] points to a linked list of cells which give the names of the nodes connected to u.
Data Structures
242-535 ADA: 8. Intro. Graphs
40
• How much storage is required?o The degree of a vertex v == number of incident edges
• directed graphs have in-degree, out-degree values
• For directed graphs, the number of items in an adjacency lists is out-degree(v) = |E|
•This uses (V + E) storage
Storage Needs
242-535 ADA: 8. Intro. Graphs
41
• For undirected graphs, the number of items in the adjency list is
degree(v) = 2*|E| (the handshaking lemma)
o Why? If we mark every edge connected to every vertex, then by the end, every edge will be marked twice
• This also uses (V + E) storage
• In summary, adjacency lists use (V+E) storage
242-535 ADA: 8. Intro. Graphs
42
• Which representation is better for graphs?• The simple answer:
• dense graph – use a matrix• sparse graph – use an adjcency list
• But a more accurate answer depends on the operations that will be applied to the graph.
• We will consider three operations:o is there an edge between u and v?o find the successors of u (in a directed graph)o find the predecessors of u (in a directed graph)
3.3. Running Time: Matrix or List?
continued
242-535 ADA: 8. Intro. Graphs
43
• Adjacency matrix: O(1) to read arcs[u][v]
• Adjacency list: O(1 + E/V) // forget the |...|o O(1) to get to adj[u]o length of linked list is on average E/V
o if a sparse graph (E<<V): O(1+ E/V) => O(1)o if a dense graph (E ≈ V2): O(1+ E/V) => O(V)
Is there an edge (u,v)?
242-535 ADA: 8. Intro. Graphs
44
• Adjacency matrix: O(V) since must examine the entire row for vertex u
• Adjacency list: O(1 + (E/V)) since must look at entire list pointed to by adj[u]o if a sparse graph (E<<V): O(1+ E/V) => O(1)o if a dense graph (E ≈ V2): O(1+ E/V) => O(V)
Find u’s successors (u->v)
242-535 ADA: 8. Intro. Graphs
45
• Adjacency matrix: O(V) since must examine the entire column for vertex uo a 1 in the row for ‘t’ means that ‘t’ is a predecessor
• Adjacency list: O(E) since must examine every list pointed to by adj[]o if a sparse graph (E<<V): O(E) is fasto if a dense graph (E ≈ V2): O(E) is slow
Find u’s predecessors (t->u)
242-535 ADA: 8. Intro. Graphs
46
• Operation Dense Graph Sparse GraphFind edge Adj. Matrix EitherFind succ. Either Adj. listFind pred. Adj. Matrix Either
• As a graph gets denser, an adjacency matrix has better execution time than an adjacency list.
Summary: which is faster?
242-535 ADA: 8. Intro. Graphs
47
• The size of an adjacency matrix for a graph of V nodes is:o V2 bits (assuming 0 and 1 are stored as bits)
3.4. Storage Space: Matrix or List?
continued
242-535 ADA: 8. Intro. Graphs
48
• An adjacency list cell uses:o 32 bits for the integer, 32 bits for the pointero so, cell size = 64 bits
• Total no. of cells = total no. of edges, eo so, total size of lists = 64*E bits
• successors[] has V entries (for V verticies)o so, array size is 32*V bits
• Total size of an adjacency list data struct:64*E + 32*V
242-535 ADA: 8. Intro. Graphs
49
• An adjacency list will use less storage than an adjacency matrix when:
64*E + 32*V < V2
which is: E < V2/64 – V/2
When V is large, ignore the V/2 term:E < V2/64
Size Comparison
continued
242-535 ADA: 8. Intro. Graphs
50
• V2 is (roughly) the maximum number of edges.
• So if the actual number of edges in a graph is 1/64 of the maximum number of edges, then an adj. list representation will be smaller than an adj. matrix codingo but the graph must be quite sparse
242-535 ADA: 8. Intro. Graphs
51
• A (free) tree is an undirected graph T such thato T is connectedo T has no cyclesThis definition of tree is different
from the one of a rooted tree
• A forest is an undirected graph without cycles
• The connected components of a forest are trees
4. Trees and Forests
Graphs 51
Tree
Forest
242-535 ADA: 8. Intro. Graphs
52
Uses of TreesPresident
Vice-Presidentfor Academics
Vice-Presidentfor Admin.
Dean ofEngineering
Dean ofBusiness
Head of CoE Head of EE Head of AC.
PlanningOfficer
PurchasesOfficer
. . . .. . . .
. . . .
242-535 ADA: 8. Intro. Graphs
53
• Non-rooted (free) treeso a free tree is a graph with no cycles
Saturated HydrocarbonsH
HH
H
C
HH C
HH C
HH CH C
H
H HH C
H
HC
H
H
C
H
Butane
Isobutane
242-535 ADA: 8. Intro. Graphs
54
A Computer File System/
usr bin tmp
bin ad spool ls mail who junk
ed vi exs opr uucp
printer
242-535 ADA: 8. Intro. Graphs
55
• e.g. Part of the ancient Greek god family:
5. (Rooted) Tree Terminology
Uranus
Aphrodite Kronos Atlas Prometheus
Eros Zeus Poseidon Hades Ares
Apollo Athena Hermes Heracles
levels0
1
2
3::
242-535 ADA: 8. Intro. Graphs
56
• Let T be a tree with root v0.• Suppose that x, y, z are verticies in T.• (v0, v1,..., vn) is a simple path in T (no loops).
• a) vn-1 is the parent of vn.• b) v0, ..., vn-1 are ancestors of vn
• c) vn is a child of vn-1
Some Definitions
continued
242-535 ADA: 8. Intro. Graphs
57
• d) If x is an ancestor of y, then y is a descendant of x.
• e) If x and y are children of z, then x and y are siblings.
• f) If x has no children, then x is a terminal vertex (or a leaf).
• g) If x is not a terminal vertex, then x is an internal (or branch) vertex.
continued
242-535 ADA: 8. Intro. Graphs
58
• h) The subtree of T rooted at x is the graph with vertex set V and edge set Eo V contains x and all the descendents of xo E = {e | e is an edge on a simple path from x to some
vertex in V}
• i) The length of a path is the number of edges it uses, not verticies.
continued
242-535 ADA: 8. Intro. Graphs
59
• j) The level of a vertex x is the length of the simple path from the root to x.
• k) The height of a vertex x is the length of the simple path from x to the farthest leafo the height of a tree is the height of its root
• l) A tree where every internal vertex has exactly m children is called a full m-ary tree.
242-535 ADA: 8. Intro. Graphs
60
• The root is Uranus.• A simple path is {Uranus, Aphrodite, Eros}
• The parent of Eros is Aphrodite.• The ancestors of Hermes are Zeus, Kronos, and
Uranus.• The children of Zeus are Apollo, Athena, Hermes,
and Heracles.
Applied to the Example
continued
242-535 ADA: 8. Intro. Graphs
61
• The descendants of Kronos are Zeus, Poseidon, Hades, Ares, Apollo, Athena, Hermes, and Heracles.
• The leaves (terminal verticies) are Eros, Apollo, Athena, Hermes, Heracles, Poseidon, Hades, Ares, Atlas, and Prometheus.
• The branches (internal verticies) are Uranus, Aphrodite, Kronos, and Zeus.
continued
242-535 ADA: 8. Intro. Graphs
62
• The subtree rooted at Kronos:
Kronos
Zeus Poseidon Hades Ares
Apollo Athena Hermes Heracles
continued
242-535 ADA: 8. Intro. Graphs
63
• The length of the path {Uranus, Aphrodite, Eros} is 2 (not 3).
• The level of Ares is 2.
• The height of the tree is 3.