2
2 Objectives Learn about graphs Become familiar with the basic terminology of graph theory Discover how to represent graphs in computer memory Examine and implement various graph traversal algorithms

3
3 Graph Definitions and Notations Graph G pair –G = (V, E), where V is a finite nonempty set –Called the set of vertices of G, and E   V x V Elements of E –Pairs of elements of V E: set of edges of G G called trivial if it has only one vertex Directed graph (digraph) –Elements in set of edges of graph G: ordered Undirected graph: not ordered

6
6 Graph Definitions and Notations Let u and v be two vertices in G –u and v adjacent If edge from one to the other exists: (u, v)  E Loop –Edge incident on a single vertex e 1 and e 2 called parallel edges –edges e 1 and e 2 associate with same pair of vertices {u, v} Simple graph –No loops, no parallel edges ABC D ABC D

7
7 Graph Definitions and Notations Let e = (u, v) be an edge in G –Edge e is incident on the vertices u and v –Degree of u written deg(u) or d(u) Number of edges incident with u Each loop on vertex u –Contributes two to the degree of u u is called an even (odd) degree vertex –If the degree of u is even (odd) ABC D

8
8 Graph Definitions and Notations Path from u to v –If sequence of vertices u 1, u 2,..., u n exists Such that u = u 1, u n = v and (u i, u i + 1) is an edge for all i =1, 2,..., n – 1 Vertices u and v called connected –If path from u to v exists Simple path –All vertices distinct (except possibly first, last) – A to C / A to B / A to D/ D to C/ D to B/ B to C/ B to D/ C to B / C to D –There is no path to A Cycle in G –Simple path in which first and last vertices are the same –A Cycle such as B C D B ABC D

9
9 Graph Definitions and Notations G is connected –If path from any vertex to any other vertex exists Let G be a directed graph and let u and v be two vertices in G –If edge from u to v exists: (u, v)  E u is adjacent to v v is adjacent from u »A adjacent to B but B adjacent from A ABC D ABC D

10
10 Graph Definitions and Notations Definitions of paths and cycles in G –Similar to those for undirected graphs G is strongly connected –If any two vertices in G are connected

17
Data Structures Using C++ 2E17 Depth First Traversal (cont’d.) General algorithm for depth first traversal at a given node v –Recursive algorithm

18
18 Breadth First Traversal Similar to traversing binary tree level-by-level –Nodes at each level Visited from left to right –All nodes at any level i Visited before visiting nodes at level i + one A B C DE F G A CD E G F E F AC D EGFB B A D H H H

22
Shortest Path (greedy algorithm) Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph uses the weight to represent the distance between two places Weight of the path P –Given G as a weighted graph with vertices u and v in G and P as a path in G from u to v Sum of the weights of all the edges on the path Shortest path: path with the smallest weight Data Structures Using C++ 2E22

25
Data Structures Using C++ 2E25 Shortest Path (cont’d.) General algorithm (cont’d.) –For each vertex w in G, such that the shortest path from vertex to w has not been determined and an edge (v, w) exists If weight of the path to w via v smaller than its current weight Update weight of w to the weight of v + weight of edge (v, w)