1 / 10

Data Structures

Andreas Savva. Data Structures. Chapter 12 Graphs. H. A. H. B. H. C. Berlin. London. Vienna. C. C. C. Paris. D. Athens. E. Lisbon. C. C. Rome. Madrid. Larnaka. F. C. H. H. Olympic Airways air routes. Message transmission in a network. Benzene molecule. H. Graphs.

megan
Download Presentation

Data Structures

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. Andreas Savva Data Structures Chapter 12 Graphs

  2. H A H B H C Berlin London Vienna C C C Paris D Athens E Lisbon C C Rome Madrid Larnaka F C H H Olympic Airways air routes Message transmission in a network Benzene molecule H Graphs Definition • A graphconsists of a set of vertices together with a set of edges. If e = (v,w) is an edge with vertices v and w, then v and w lie on e and they are said to be adjacent vertices. • Undirected graph (just graph) • Directed graph (digraph)

  3. Undirected Graphs • A path is a sequence of distinct vertices, each adjacent to the next. • A graph is called connected if there is a path from any vertex to any other vertex. • A cycle is a path containing at least three vertices such that the last vertex on the path is adjacent to the first. • If a graph is disconnected, then the maximal subset of connected vertices is called a component. • A graph with no cycles is a tree.

  4. 2 1 2 1 2 1 4 4 3 3 4 3 2 1 2 1 4 3 4 3 Undirected Graphs Connected Path Cycle Disconnected 2 components Tree

  5. Directed Graphs • A directed path is a sequence of distinct vertices, each adjacent to the next always moving in the direction indicated by the arrows. • A directed cycle is a directed path containing at least three vertices such that the last vertex on the path is adjacent to the first. • A directed graph is called strongly connected if there is a directed path from any vertex to any other vertex. • If we ignore the direction of the edges and the resulting undirected graph is connected, we call the directed graph weakly connected.

  6. Directed Graphs Directed cycle Strongly connected Weakly connected

  7. Graph Traversal • In many problems, we wish to investigate all the vertices in a graph in some systematic order, just as with binary trees, where we developed several systematic traversal methods. • Depth-first traversal of a graph • Breath-First traversal

  8. Start Depth-first Traversal • Depth-first traversal of a graph is analogous to preorder traversal of an ordered tree. • Algorithm: • Suppose that the traversal has just visited a vertex v and let w1 , w2 , … , wk be the vertices adjacent to v. Then it next visits w1 and keep w2 , … , wk waiting. After visiting w1 we traverse all the vertices to which it is adjacent before returning to traverse w2 , … , wk . 0 1 2 4 8 3 5 7 6 Strongly connected

  9. Start Breadth-first Traversal • Breadth-first traversal of a graph is analogous to level-by-level traversal of an ordered tree. • Algorithm: • Suppose that the traversal has just visited a vertex v and let w1 , w2 , … , wk be the vertices adjacent to v. Then it next visits all the vertices adjacent to v , putting the vertices adjacent to these in a waiting list to be traversed after all vertices adjacent to v have been visited. 0 1 4 2 3 6 5 8 7 Strongly connected

  10. Traversal of a Tree 0 0 1 7 1 2 2 4 8 11 3 4 5 6 3 5 10 12 7 8 11 12 6 9 13 9 10 13 Depth-first traversal Breadth-first traversal

More Related