400 likes | 413 Views
This lecture introduces the basics of graph theory and its applications in computer science. Topics include graph representation, path and cycle, graph types, and graph traversal algorithms.
E N D
Agenda • Lecture Content: • Introduction to Graph • Path and Cycle • Euler & Hamiltonian Cycle • Exercise
Pengumuman QUIZ 2 Minggu Depan
Modelled as A Graph • Vertices: the dots with names • Edges: the lines that connect the vertices • Path from Sheridan to Gillette: Sheridan, Buffalo, Gillette. Graph-Theoretic question Is there a path from vertex Greybull to vertex Greybull that traverses every edge exactly once?
Applications of Graph Theory in Computer Science • Computer network • Operating systems • Encryption • Channel allocation, scheduling • Relational database • Coding and compression • Parallel computation • … etc • Maps the problem to graph, states graph-theoretic problem, solves using: • Well-known solution in graph theory • If no solution exists, use heuritics
Definition A graph (or undirected graph) G consists of a set V of vertices (or nodes) and a set E of edges (or arcs) such that each edge e E is associated with an unordered pair of vertices. If there is a unique edge e associated with the vertices v and w, we write e = (v, w) or e = (w, v) .
Example: Computer Network A network is made up of data centers and communication links between computers. We can represent the location of each data center by a point and each communications link by a line segment.
Definition A directed graph (or digraph) G consists of a set V of vertices (or nodes) and a set E of edges (or arcs) such that each edge e E is associated with an ordered pair of vertices. If there is a unique edge e associated with the ordered paired (v, w) of vertices, we write e = (v, w) which denotes an edge from v to w).
Example: Computer Network Directed graphs: if we assign directions to the edges of a graph. The ordered pair (v, w): said to start at v and end at w.
Definition • An edge e in a graph (undirected or directed) that is associated with the pair of vertices v and w is said to be incident on v and w. Vertices v and w are adjacent vertices, and are said to be incident on e. • If G is a graph (undirected or directed) with vertices V and edges E, we write G = (V, E) • Example using Wyoming Highway System: • G consists of the set: • V = {Gre, She, Wor, Buf, Gil, Sho, Cas, Dou, Lan, Mud} • E = {e1, e2, …, e13}
Definition Parallel edges: both edge e1 and e2 are associated with the vertex pair {v1, v2}. Loop: an edge incident on a single vertex. Isolated vertex: a vertex that is not incident on any edge. Simple graph: a graph with neither loops nor parallel edges. Is the one below a simple graph?
Weighted Graph Weighted graph: a graph with numbers on the edges. The number is called weight of edge. Length of a path: the sum of the weights of the edges in the path. The optimal path: a path of minimum length that visits every vertex exactly one time.
Definition: The complete graph on n vertices, denoted Kn, is the simple graph with n vertices in which there is an edge between every pair of distinct vertices. Example: K4
Bipartite Graph A graph G = (V, E) is bipartite if there exists subsets V1 and V2 of V such that V1 V2 = , V1 V2 = V, and each edge in E is incident on one vertex in V1 and one vertex in V2.
Complete Bipartite Graph The complete bipartite graph on m and n vertices, denoted Km,n is the simple graph whose vertex set is partitioned into sets V1 with m vertices and V2 with n vertices in which the edge set consists of all edges of the form (v1, v2) with v1 V1 and v2 V2 Example: K2,4
Paths: Illustration Vertices: cities Edges: roads Paths: a trip beginning at some city, passing through several cities, and terminating at some city. Let vo and vn be vertices in a graph and ei is incident on vertices vi-1 and vi. A path from vo to vn : (vo, e1, v1, e2, ……, vn-1, en, vn)
Paths • Definition • Let v0 and vn be vertices in a graph. • A path from v0 to vn of length n is an alternating sequence of n+1 vertices and n edges beginning with vertex v0 and ending with vertex vn, (v0,e1,v1,e2,v2,...,vn-1,en,vn) , in which edge ei is incident on vertices vi-1and vi for i = 1, ..., n. • Example • Find a path of length 4 from vertex 1 to vertex 2 in the following graph:
Example (1, e1, 2, e2, 3, e3, 4, e4, 2) is a path of length 4 from 1 to 2 If there are no parallel edges (1, 2, 3, 4, 2) Is (6) a path? Yes, it is a path of length 0 from 6 to 6 “path” allows repetitions of vertices or edges or both.
Paths and Connected Graph • Note • In the absence of parallel edges, in denoting a path we may suppress the edges. • Previous example: (1,2,3,4,2) • Definition • A graph G is connected if given any vertices v and w in G, there is a path from v to w. • Examples • Are the following graphs connected?
Subgraph • Definition • Let G = (V,E) be a graph. • We call (V’,E’) a subgraph of G if (a) V’ V and E’ E. (b) For every edge e’ E’, if e’ is incident on v’ and w’, then v’, w’ V’. • Examples • Is the graph G’ = (V’,E’) a subgraph of the graph G = (V,E)
Subgraph: Example • Find all subgraphshaving at least one vertex of the following graph. v2 e1 v1
Component • Definition • Let G be a graph and let v be a vertex in G. • The subgraph G’ of G consisting of all edges and vertices in G that are contained in some path beginning at v is called the component of G containing v. • Examples • Find the components of this graph which contains • v3 • v4 • v5
Simple Paths and Cycles • Definition • Let v and w be vertices in a graph G. • A simple path from v to w is a path from v to w with no repeated vertices. • A cycle(or circuit) is a path of nonzero length from v to v with no repeated edges. • A simple cycle is a cycle from v to v in which, except for the beginning and ending vertices that are both equal to v, there are no repeated vertices.
Euler Cycles A cycle in a graph G that includes all of the edges and all of the vertices of G. (v6, v4, v7, v5, v1, v3, v4, v1, v2, v5, v4, v2, v3, v6) If a graph G has an Euler cycle, then G is connected and every vertex has even degree. The degree of a vertex v = d(v) is the number of edges incident on v
Paths and Cycles • Theorem • If a graph G has an Euler cycle, then G is connected and every vertex has even degree. • Theorem • If G is a connected graph and every vertex has even degree, then G has an Euler cycle.
Paths and Cycles • Theorem • If G is a graph with m edges and vertices {v1, v2, ..., vn}, then Σind(vi) = 2m. In particular, the sum of the degrees of all the vertices in a graph is even. • Corollary • In any graph, there are an even number of vertices of odd degree. • Theorem • A graph has a path with no repeated edges from v to w (v≠ w) containing all the edges and vertices if and only if it is connected and v and w are the only vertices having odd degree.
Hamiltonian Cycle • Hamiltonian cycle • A cycle in a graph G that contains each vertex in G exactly once, except for the starting and ending vertex that appears twice. • Examples • Determine if the following graphs have a Hamiltonian cycle.
Euler vs Hamiltonian Cycle An Euler Cycle visits each edge once. A Hamiltonian Cycle visits each vertex once. Hamiltonian cycle: (a, b, c, d, e, f, g, a) Euler cycle?
Graph with No Hamiltonian Cycle Why? There are 5 vertices, Hamiltonian cycle must have 5 edges, and each vertex has degree 2 Try to eliminate edges. Can you still have 5 edges?
Travelling Salesperson Problem Find a shortest route in which the salesperson can visit each city one time, starting and ending at the same city. Solution: Given a weighted graph G, find a minimum-length Hamilton cycle in G.
Travelling Salesperson Problem: Example List cycles, and pick the one with the shortest length .. Delete edge with high cost, we get: Cmin = (a, b, c, d, a) length = 2+3+3+2=10