180 likes | 451 Views
Graphs. CS 2606. Topics. Graph notation revisited Undirected graph Directed graph Weighted/labeled graph More graph problems Euler Tour Hamiltonian cycle Max Clique Traveling salesman. Undirected Graph. Graph G=(V,E): V={a,b,c,d}, E={(a,b),(b,c),(b,d),(a,d)}. a. b. c. d.
E N D
Graphs CS 2606
Topics • Graph notation revisited • Undirected graph • Directed graph • Weighted/labeled graph • More graph problems • Euler Tour • Hamiltonian cycle • Max Clique • Traveling salesman
Undirected Graph Graph G=(V,E): V={a,b,c,d}, E={(a,b),(b,c),(b,d),(a,d)} a b c d
Directed Graph Edge set is a set of ordered pairs Graph G=(V,E): V={a,b,c,d}, E={(a,b),(b,c),(b,d),(d,b),(d,a)} a b c d
Weighted Graph Graph G=(V,E,w): V={a,b,c,d,e}, E={(a,b),(a,d),(b,c),(b,d),(b,e)},w: function that maps vertex-pairs to values (can be stored in adjacency matrix) w(a,b) = w(b,a) = 20w(a,d) = w(d,a) = 65w(b,c) = w(c,b) = 32w(b,d) = w(d,b) = 12w(b,e) = w(e,b) = 35 for all othervertex pairs:w(x,y) = 0 if x = yelse, w(x,y) = infinity a 20 35 e b 12 32 c 65 d
Graph problems • Euler tour • Find a sequence of vertices that traverse all edges in the graph exactly once • Hamiltonian cycle • Find a simple cycle that connects all vertices in the graph • Max Clique • Find the largest subset of vertices that are all pair-wise adjacent (connected by an edge)
Graph problems 2 • Shortest Paths • Section 11.4 of textbook • Minimum Cost Spanning Tree • Section 11.5 of textbook • Traveling Salesman • Given a weighted graph G, find the shortest (minimum-length) cycle that contains all vertices in G
Euler Tour • Euler Tour/Cycle: sequence of vertices that traverse all edges exactly once c d a b f e Euler tour: c,d,f,e,a,b,e,c
Euler Tour (2) • Some graphs do not have euler tours • specifically those with vertices whose degree (# of neighbors) is odd c a b d e g f f,c,d and e have odd degree
Euler Tour (3) • There is a straightforward algorithm that obtains an euler tour of a graph, if one such tour exists • Algorithm sketch • Start with any vertex; traverse edges by repeatedly visiting neighbors without repeating an edge • Repeat process, combining the partial tours as you go, until all edges are exhausted
Hamiltonian Cycle • Hamiltonian cycle: simple cycle containing all vertices w c a b z d x y f e no hamiltonian cycle hamiltonian cycle:a,b,c,d,f,e,(then back to a)
Hamiltonian Cycle (2) • Exhaustive (inefficient) algorithm that finds a hamiltonian cycle if one such cycle exists: • Consider all permutations of the vertices • Return the permutation that forms a cycle • An O( n! n ) algorithm • Hamiltonian cycle is an example of anNP-complete (intractable) problem • No algorithm that performs better(than the exhaustive algorithm) is known
Max Clique • Clique: subset of vertices that are all connected by an edge Clique examples: {a,b,e}{e,g}{b,c,d,f} Max-clique:{b,c,d,f} c a b d e f g h
Max Clique (2) • Exhaustive algorithm for Max Clique: • Consider all subsets of vertices • Determine if the subset is a clique • Return the clique with the largest size • An O( 2n n2 ) algorithm • Max Clique is also NP-Complete
Traveling Salesman • Find shortest tour of all vertices (may impose that the salesman end where he started) w Sample tours w,x,y,z,w – length: 34y,w,x,z,y – length: 36z,w,y,x,z – length: 28 4 10 14 y 3 7 z x 11
Traveling Salesman (2) • O( n! n ) algorithm: • Consider all permutations • Compute resulting length for each permutation • Return the permutation that yields the shortest length • Traveling salesman is also NP-complete
More on NP-Completeness • See chapter 15 of the textbook