110 likes | 244 Vues
Explore the concepts of graphs, both directed and undirected, including paths, cycles, components, and traversal methods such as Depth-first and Breadth-first. Learn about the structure and properties of graphs.
E N D
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 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)
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.
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
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.
Directed Graphs Directed cycle Strongly connected Weakly connected
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
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
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
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