440 likes | 448 Views
Learn about graphs, vertices, edges, directed vs. undirected graphs, paths, subgraphs, and connectivity concepts in computer science. Explore how graphs represent data relationships and solve complex questions. Dive into airline flight scheduling and shortest distance calculations.
E N D
COSC 2007Data Structures II Chapter 14 Graphs I
Topics • Introduction & Terminology • ADT Graph
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?
B C A F D E Terminology and Notations • 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)}
B C A F D E Terminology and Notations • If edge pairs are ordered, the graph is directed, otherwise undirected. • We draw edges in undirected graphs with lines with no arrow heads. (B, C) and (C, B) mean the same edge This is an undirected graph.
B C A F D E Terminology and Notations • If edge pairs are ordered, the graph is directed, otherwise undirected. • We draw edges in directed graphs with lines with arrow heads. This edge is (B, C). (C, B) wouldmean a directed edge from C to B This is a directed graph.
B C A F D E Terminology and Notations • 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 This is a directed graph.
B C A F D E Terminology and Notations • 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.
B C A F D E Terminology and Notations • 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.
3 B C A 4.5 1.2 7.5 F – 2.3 D E Terminology and Notations • 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
4 3 B C A 4.5 1.2 7.5 F – 2.3 D E Terminology and Notations • 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) =
B C A F D E Terminology and Notations • Let G = (V, E) be a graph. • A subgraph of G is a graph H = (V*, E*) such that V* V and E* E. E.g., V* = {A, C, D}, E* = {(C, D)}.
C A D Terminology and Notations • Let G = (V, E) be a graph. • A subgraphof G is a graph H = (V*, E*) such that V* V and E* E. E.g., V* = {A, C, D}, E* = {(C, D)}.
B C A F D E Terminology and Notations • 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 for1<= i <= N–1. 1 2 N i i+1 E.g., A, B, C, E is a path in this graph
B C A F D E Terminology and Notations • 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. 1 2 N E.g., the length of path A, B, C, E is 3.
B C A F D E Terminology and Notations • 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. 1 2 N i i+1 E.g., A, B, C, E is a path in this directed graph, but . . . . . . but A, B, C, D is not a path, since (C, D) is not an edge.
B C A F D E Terminology and Notations A path is simple if all vertices in it are distinct, except that the first and last could be the same. E.g., the path A, B, C, E is simple . . . . . . and so is the path A, B, C, E, D, A.
B C A F D E Terminology and Notations If G is an undirected graph, we say it is connectedif there is a PATH from every vertex to every other vertex. This undirected graph is not connected.
B C A F D E Terminology and Notations If G is an directed graph, we say it is strongly connectedif there is a path from every vertex to every other vertex. This directed graph is strongly connected.
B C A F D E Terminology and Notations If G is an directed graph, we say it is strongly connectedif 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.
B C A F D E Terminology and Notations 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 notstrongly connected, but it is weakly connected, since . . .
B C A F D E Terminology and Notations 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.
B C A F D E Terminology and Notations • Cycle: a path that begins and ends at the same node but doesn't pass through other nodes more than once The path A, B, C, E, D, A is a cycle.
B C A F D E Terminology and Notations • A graph with no cycles is called acyclic. This graph is acyclic.
B C A F D E Terminology and Notations • A graph with no cycles is called acyclic. This directed graph is not acyclic, . . .
B C A F D E Terminology and Notations • A graph with no cycles is called acyclic. . . . but this one is. A Directed Acyclic Graph is often called simply a DAG.
B C A D E Terminology and Notations • A complete graph is one that has an edge between every pair of vertices. Incomplete:
B C A D E Terminology and Notations • 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:
B C A D E Terminology and Notations • 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|? This graph has |V| = 5 vertices and |E| = 10 edges. Complete:
B C A F D E Terminology and Notations • A free treeis a connected, acyclic, undirected graph. • “Free” refers to the fact that there is no vertex designated as the “root.” This is a free tree.
root B C A F D E Terminology and Notations • A free treeis a connected, acyclic, undirected graph. • If some vertex is designated as the root, we have a rootedtree.
H K G B C A I F J L D E Terminology and Notations • If an undirected graph is acyclic but possibly disconnected, it is a forest. This is a forest. It contains three free trees.
H K G B C A I F J L D E Terminology and Notations • 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.
Review • In a graph, a vertex is also known as a(n) ______. • node • edge • path • cycle
Review • A graph consists of ______ sets. • two • three • four • five
Review • A subset of a graph’s vertices and edges is known as a ______. • bar graph • line graph • Subgraph • circuit
Review • Two vertices that are joined by an edge are said to be ______ each other. • related to • bordering • utilizing • adjacent to
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
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
Review • A graph is ______ if each pair of distinct vertices has a path between them. • complete • disconnected • connected • full
Review • A complete graph has a(n) ______ between each pair of distinct vertices. • edge • path • Cycle • circuit
Review • The ______ of a weighted graph have numeric labels. • vertices • edges • paths • cycles
Review • The edges in a ______ indicate a direction. • graph • multigraph • digraph • spanning tree
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