1 / 44

COSC 2007 Data Structures II

COSC 2007 Data 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

terryroy
Télécharger la présentation

COSC 2007 Data Structures II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COSC 2007Data 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. 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)}

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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

  11. 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) = 

  12. 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)}.

  13. 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)}.

  14. 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

  15. 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.

  16. 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.

  17. 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.

  18. 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.

  19. 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.

  20. 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.

  21. 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 . . .

  22. 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.

  23. 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.

  24. B C A F D E Terminology and Notations • A graph with no cycles is called acyclic. This graph is acyclic.

  25. B C A F D E Terminology and Notations • A graph with no cycles is called acyclic. This directed graph is not acyclic, . . .

  26. 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.

  27. B C A D E Terminology and Notations • A complete graph is one that has an edge between every pair of vertices. Incomplete:

  28. 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:

  29. 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:

  30. 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.

  31. 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.

  32. 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.

  33. 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.

  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

More Related