Date post: | 13-Dec-2015 |
Category: |
Documents |
Upload: | jeffry-greene |
View: | 233 times |
Download: | 0 times |
1/24
Introduction to Graphs
2/24
Graph Definition• Graph : consists of vertices and edges. Each
edge must start and end at a vertex.
• Graph G = (V, E)– V = set of vertices– E = set of edges (VV)
• Example:
www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt
3/24
Graph Definition
• Degree of a vertex : number of edges connected to it
• Example: V4 has degree = 3
V2
V5
V3
V4
V1
www.cod.edu/people/faculty/hubbardd/presentations/Math%201218/MTH1218%20Chapter%204.ppt
4/24
Graph Definition
• Total degree of a graph : sum of the degrees of all the vertices. Note: If a graph has n edges, the total degree = 2n.
• Example: graph has total degree = 10
V2
V3V4
V1
www.cod.edu/people/faculty/hubbardd/presentations/Math%201218/MTH1218%20Chapter%204.ppt
5/24
Graph Definition
• The Handshaking Theorem:– Let G = (V, E). Then
– Proof:• Each edge contributes twice to the degree count of all
vertices.
– Example:• If a graph has 5 vertices, can each vertex have degree
3? Or 4?– The sum is 53 = 15 which is an odd number. Not possible.– The sum is 54 = 20 = 2 |E| and 20/2 = 10 edges. May be possible.
EvVv
2)deg(
www.cs.sfu.ca/~jcliu/MACM101/09-Graph.ppt
6/24
Graph Definition
• Types of graphs– Undirected: edge (u, v) = (v, u); for all v,
» (v, v) E (usually no self loops)
» Some may have self loops.
– Directed: (u, v) is edge from u to v, denoted as u v. • Self loops are allowed.• Is a graph where an edge represents a one-way relation only.
– Cf. undirected graph – an edge represents two-way or symmetric relationship between two vertices.
• The number of directed edges which initiate from vertex v is called the outdegree of v or outdeg(v).
• The number of directed edges which terminate at vertex v is called the indegree of v or indeg(v).
www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt
7/24
Representation of Graphs
• Two standard ways– Adjacency Lists.
– Adjacency Matrix.
a
dc
b
a
b
c
d
b
a
d
d c
c
a b
a c
a
dc
b1 2
3 4
1 2 3 41 0 1 1 12 1 0 1 03 1 1 0 14 1 0 1 0
www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt
8/24
Adjacency Lists• Consists of an array Adj of |V| lists.• One list per vertex.• For u V, Adj[u] consists of all vertices adjacent to u.
a
dc
b
a
b
c
d
b
c
d
d c
a
dc
b
a
b
c
d
b
a
d
d c
c
a b
a c
If weighted, also store weights in adjacency lists.
www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt
9/24
Storage Requirement
• For directed graphs:– Sum of lengths of all adj. lists is
out-degree(v) = |E|
vV
– Total storage: (|V|+|E|)• For undirected graphs:
– Sum of lengths of all adj. lists is
degree(v) = 2|E|
vV
– Total storage: (|V|+|E|)
No. of edges leaving v
No. of edges incident on v. Edge (u,v) is incident on vertices u and v.
www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt
10/24
Pros and Cons: adj list
• Pros– Space-efficient, when a graph is sparse.– Can be modified to support many graph variants.
• Cons– Determining if an edge (u,v) G is not efficient.
• Have to search in u’s adjacency list. (degree(u)) time. (V) in the worst case.
www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt
11/24
Adjacency Matrix• |V| |V| matrix A.• Number vertices from 1 to |V| in some arbitrary
manner.• A is then given by:
otherwise0
),( if1],[
EjiajiA ij
a
dc
b1 2
3 4
1 2 3 41 0 1 1 12 0 0 1 03 0 0 0 14 0 0 0 0
a
dc
b1 2
3 4
1 2 3 41 0 1 1 12 1 0 1 03 1 1 0 14 1 0 1 0
A = AT for undirected graphs.
www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt
If weighted, store weights also in adjacency matrix.
12/24
Space and Time
• Space: (|V|2).– Not memory efficient for large graphs.
• Time: to list all vertices adjacent to u: (|V|).• Time: to determine if (u, v) E: (1).• Can store weights instead of bits for weighted
graph.
www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt
13/24
Directed Graphs
– Theorem
– Examples (continued):• Adjacency matrix of a directed graph:
– outdeg(V1) = 1, indeg(V1) = 2
– outdeg(V2) = 2, indeg(V2) = 1
– outdeg(V3) = 0, indeg(V3) = 2
– outdeg(V4) = 2, indeg(V4) = 0
VvVv
vvE )outdeg()indeg(
www.cs.unc.edu/~plaisted/comp122/19-graph1.ppt
14/24
Each edge e has a weight, wt(e)• graph may be undirected or directed• weight may represent length, cost, capacity, etc• adjacency matrix becomes weight matrix• adjacency lists include weight in node
4
6
6 7
4
5
75
5
8
v w
yx
z
a weighted graph G
www.dcs.gla.ac.uk/~rwi/alg3/Lecture6.ppt
Weighted Graphs
15/24
Vertex cover
• A set of vertices that cover all edges, i.e., at least one vertex of all edges is in the set.
• I.e., set of vertices WV with for all {x,y} E: x W or y W.
• Vertex Cover problem:– Given G, find vertex cover of minimum size
Modified from www.cs.uu.nl/docs/vakken/na/na10-2005b.ppt
16/24
Subset S of vertices such that no two vertices in S are connected
www.cs.rochester.edu/~stefanko/Teaching/06CS282/06-CSC282-17.ppt
Independent Set (I)
17/24
Subset S of vertices such that no two vertices in S are connected
www.cs.rochester.edu/~stefanko/Teaching/06CS282/06-CSC282-17.ppt
Independent Set (II)
18/24
•INSTANCE: graph G•SOLUTION: independent set S in G•MEASURE: maximize the size of S
•INSTANCE: graph G, number K•QUESTION: does G have independent set of size K
OPTIMIZATION VERSION:
DECISION VERSION:
www.cs.rochester.edu/~stefanko/Teaching/06CS282/06-CSC282-17.ppt
Independent Set (III)
19/24
Subset S of vertices such that every two vertices in S
are connected www.cs.rochester.edu/~stefanko/Teaching/06CS282/06-CSC282-17.ppt
Clique (I)
20/24
Clique
INSTANCE: graph G, number KQUESTION: does G have a clique of size K?
www.cs.rochester.edu/~stefanko/Teaching/06CS282/06-CSC282-17.ppt
Clique (II)
21/24
Relations
• The following are equivalent– G has an independent set with at least k
vertices– The complement of G has a clique with at
least k vertices– G has a vertex cover with at most n-k vertices
www.cs.uu.nl/docs/vakken/na/na10-2005b.ppt
22/24
Bipartite Graphs
• A bipartite graph G is– a graph in which the vertices V can be partitioned into
two disjoint subsets V1 and V2 such that
• no two vertices in V1 are adjacent;
• no two vertices in V2 are adjacent.
• A complete bipartite graph Km,n is– Is a bipartite graph in which the sets V1 and V2 contain
m and n vertices, respectively, and every vertex in V1 is adjacent to every vertex in V2.
– Q: How many edges ?
http://www.cs.sfu.ca/~jcliu/MACM101/09-Graph.ppt
23/24
Assignment Problem
• Assignment problem.– Input: weighted, complete bipartite graph G =
(L R, E)with |L| = |R|.
– Goal: find a perfect matching of min weight.
1
2
3
4
5
1' 2' 3' 4' 5'
Min cost perfect matching
M = { 1-2', 2-3', 3-5', 4-1', 5-
4' }
cost(M) = 8 + 7 + 10 + 8 + 11
= 44
3 8 9 15 10
4 10 7 16 14
9 13 11 19 10
8 13 12 20 13
1 7 5 11 9www.bioalgorithms.infoAn Introduction to Bioinformatics Algorithms
24/24
Assignment problem and related problems
• L: n jobs, R: m machines, weight of an edge (i,j) in E: time for job i taken on machine j. – Assign n jobs to each of m machines such that the
total processing time is minimized.• Matching: is a subset of E such that no two
edges in the subset are adjacent. – Perfect matching: A matching saturates all vertices.– Maximum matching: A matching with the largest
possible number of edges.• Min-cost maximum matching problem: Find a
max matching in a graph (with edge weights) such that the total edge weight is minimized.