# Data Structures Chapter 12 Graphs Andreas Savva. 2 Definition A graph consists of a set of vertices together with a set of edges. If e = (v,w) is an edge.

## Presentation on theme: "Data Structures Chapter 12 Graphs Andreas Savva. 2 Definition A graph consists of a set of vertices together with a set of edges. If e = (v,w) is an edge."— Presentation transcript:

Data Structures Chapter 12 Graphs Andreas Savva

2 Definition A graph consists 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. A graph consists 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) Graphs C CC C CC H H H H H H Benzene molecule London Athens Larnaka Paris Vienna Berlin Rome Madrid Lisbon Olympic Airways air routes A B C D E F Message transmission in a network

3 Undirected Graphs A path is a sequence of distinct vertices, each adjacent to the next. 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 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. 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. If a graph is disconnected, then the maximal subset of connected vertices is called a component. A graph with no cycles is a tree. A graph with no cycles is a tree.

4 Undirected Graphs 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 ConnectedPathCycle 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 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 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. 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. 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. 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 Depth-first Traversal Depth-first traversal of a graph is analogous to preorder traversal of an ordered tree. Depth-first traversal of a graph is analogous to preorder traversal of an ordered tree. Algorithm: Algorithm: Suppose that the traversal has just visited a vertex v and let w 1, w 2, …, w k be the vertices adjacent to v. Then it next visits w 1 and keep w 2, …, w k waiting. After visiting w 1 we traverse all the vertices to which it is adjacent before returning to traverse w 2, …, w k. Suppose that the traversal has just visited a vertex v and let w 1, w 2, …, w k be the vertices adjacent to v. Then it next visits w 1 and keep w 2, …, w k waiting. After visiting w 1 we traverse all the vertices to which it is adjacent before returning to traverse w 2, …, w k. Strongly connected Start 0 12 3 4 5 6 7 8