COSC 2007 Data Structures II Chapter 14 Graphs I
2 Topics Introduction & Terminology ADT Graph
3 Introduction Graphs Important mathematical concept that have significant application in computer science Can be viewed as a data structure or ADT Provide a way to represent relationships between data Questions Answered by Using Graphs: Airline flight scheduling: What is the shortest distance between two cities?
4 A graph G is a pair (V, E) where V is a set of vertices (nodes) V = {A, B, C, D, E, F} E is a set of edges (connect vertex) E = {(A,B), (A,D), (B,C),(C,D), (C,E), (D,E)} A B C F D E Terminology and Notations
5 If edge pairs are ordered, the graph is directed, otherwise undirected. We draw edges in undirected graphs with lines with no arrow heads. This is an undirected graph. (B, C) and (C, B) mean the same edge A B C F D E Terminology and Notations
6 If edge pairs are ordered, the graph is directed, otherwise undirected. We draw edges in directed graphs with lines with arrow heads. A B C F D E This is a directed graph. This edge is (B, C). (C, B) would mean a directed edge from C to B Terminology and Notations
7 Directed Graph (Digraph): If an edge between two nodes has a direction (directed edges) Out-degree (OD) of a Node in a Digraph: The number of edges exiting from the node In-degree (ID) of a Node in a Digraph: The number of edges entering the node A B C F D E This is a directed graph. Terminology and Notations
8 Vertex w is adjacent to v if and only if (v, w) E. In a directed graph the order matters: B is adjacent to A in this graph, but A is not adjacent to B. A B C F D E Terminology and Notations
9 Vertex w is adjacent to v if and only if (v, w) E. In an undirected graph the order does not matter: we say B is adjacent to A and that A is adjacent to B. A B C F D E Terminology and Notations
10 In some cases each edge has a weight (or cost) associated with it. The costs might be determined by a cost function E.g., c(A, B) = 3, c(D,E) = – 2.3, etc. 4 A B C F D E – Terminology and Notations
11 In some cases each edge has a weight (or cost) associated with it. When no edge exists between two vertices, we say the cost is infinite. E.g., c(C,F) = A B C F D E – Terminology and Notations
12 Let G = (V, E) be a graph. A subgraph of G is a graph H = (V*, E*) such that V* V and E* E. A B C F D E E.g., V* = {A, C, D}, E* = {(C, D)}. Terminology and Notations
13 Let G = (V, E) be a graph. A subgraph of G is a graph H = (V*, E*) such that V* V and E* E. A C D E.g., V* = {A, C, D}, E* = {(C, D)}. Terminology and Notations
14 Let G = (V, E) be a graph. A path in the graph is a sequence of vertices w, w,..., w such that (w, w ) E for 1<= i <= N–1. 12Ni i+1 A B C F D E E.g., A, B, C, E is a path in this graph Terminology and Notations
15 Let w, w,..., w be a path. The length of the path is the number of edges, N–1, one less than the number of vertices in the path. A B C F D E E.g., the length of path A, B, C, E is 3. 12N Terminology and Notations
16 Let w, w,..., w be a path in a directed graph. Since each edge (w, w ) in the path is ordered, the arrows on the path are always directed along the path. A B C F D E 12N ii+1 E.g., A, B, C, E is a path in this directed graph, but... Terminology and Notations... but A, B, C, D is not a path, since (C, D) is not an edge.
17 A path is simple if all vertices in it are distinct, except that the first and last could be the same. A B C F D E E.g., the path A, B, C, E is simple... Terminology and Notations... and so is the path A, B, C, E, D, A.
18 If G is an undirected graph, we say it is connected if there is a PATH from every vertex to every other vertex. A B C F D E This undirected graph is not connected. Terminology and Notations
19 If G is an directed graph, we say it is strongly connected if there is a path from every vertex to every other vertex. This directed graph is strongly connected. A B C F D E Terminology and Notations
20 If G is an directed graph, we say it is strongly connected if there is a path from every vertex to every other vertex. This directed graph is not strongly connected; e.g., there’s no path from D to A. A B C F D E Terminology and Notations
21 If G is directed and not strongly connected, but the underlying graph (without direction to the edges) is connected, we say that G is weakly connected. This directed graph is not strongly connected, but it is weakly connected, since... A B C F D E Terminology and Notations
22 If G is directed and not strongly connected, but the underlying graph (without direction to the edges) is connected, we say that G is weakly connected.... since the underlying undirected graph is connected. A B C F D E Terminology and Notations
23 Cycle: a path that begins and ends at the same node but doesn't pass through other nodes more than once A B C F D E The path A, B, C, E, D, A is a cycle. Terminology and Notations
24 A graph with no cycles is called acyclic. A B C F D E This graph is acyclic. Terminology and Notations
25 A graph with no cycles is called acyclic. This directed graph is not acyclic,... A B C F D E Terminology and Notations
26 A graph with no cycles is called acyclic.... but this one is. A Directed Acyclic Graph is often called simply a DAG. A B C F D E Terminology and Notations
27 A complete graph is one that has an edge between every pair of vertices. A B C D E Incomplete: Terminology and Notations
28 A complete graph is one that has an edge between every pair of vertices. ( if the graph contains the maximum possible number of edges) A complete graph is also connected, but the converse is not true Complete: A B C D E Terminology and Notations
29 A complete graph is one that has an edge between every pair of vertices. Suppose G = (V, E) is complete. Can you express |E| as a function of |V|? Complete: A B C D E This graph has |V| = 5 vertices and |E| = 10 edges. Terminology and Notations
30 A free tree is a connected, acyclic, undirected graph. “Free” refers to the fact that there is no vertex designated as the “root.” A B C D E F Terminology and Notations This is a free tree.
31 A free tree is a connected, acyclic, undirected graph. If some vertex is designated as the root, we have a rooted tree. A B C D E F root Terminology and Notations
32 If an undirected graph is acyclic but possibly disconnected, it is a forest. A B C D E F G H I J L K This is a forest. It contains three free trees. Terminology and Notations
33 If an undirected graph is acyclic but possibly disconnected, it is a forest. This graph contains a cycle. Therefore it is neither a free tree nor a forest. A B C D E F G H I J L K Terminology and Notations
34 Review In a graph, a vertex is also known as a(n) ______. node edge path cycle
35 Review A graph consists of ______ sets. two three four five
36 Review A subset of a graph’s vertices and edges is known as a ______. bar graph line graph Subgraph circuit
37 Review Two vertices that are joined by an edge are said to be ______ each other. related to bordering utilizing adjacent to
38 Review All ______ begin and end at the same vertex and do not pass through any other vertices more than once. paths simple paths cycles simple cycles
39 Review Which of the following is true about a simple cycle? it can pass through a vertex more than once it can not pass through a vertex more than once it begins at one vertex and ends at another it passes through only one vertex
40 Review A graph is ______ if each pair of distinct vertices has a path between them. complete disconnected connected full
41 Review A complete graph has a(n) ______ between each pair of distinct vertices. edge path Cycle circuit
42 Review The ______ of a weighted graph have numeric labels. vertices edges paths cycles
43 Review The edges in a ______ indicate a direction. graph multigraph digraph spanning tree
44 Review If there is a directed edge from vertex x to vertex y, which of the following can be concluded about x and y? y is a predecessor of x x is a successor of y x is adjacent to y y is adjacent to x