What is a graph? What is a graph? Directed vs. undirected graphs Directed vs. undirected graphs Trees vs graphs Trees vs graphs Terminology: Degree of a Vertex Terminology: Degree of a Vertex Graph terminology Graph terminology Graph Traversal Graph Traversal Graph representation Graph representation
A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of edges describes relationships among the vertices
A graph G is defined as follows: G=(V,E) V(G): a finite, nonempty set of vertices E(G): a set of edges (pairs of vertices) back
When the edges in a graph have no direction, the graph is called undirected
When the edges in a graph have a direction, the graph is called directed (or digraph) E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7) Warning: if the graph is directed, the order of the vertices in each edge is important !! back
Trees are special cases of graphs !! back
The degree of a vertex is the number of edges incident to that vertex For directed graph, the in-degree of a vertex v is the number of edges that have v as the head the out-degree of a vertex v is the number of edges that have v as the tail if di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is
G1G1 G2G directed graph in-degree out-degree G3G3 in:1, out: 1 in: 1, out: 2 in: 1, out: Examples back
Adjacent nodes: two nodes are adjacent if they are connected by an edge Path: a sequence of vertices that connect two nodes in a graph Complete graph: a graph in which every vertex is directly connected to every other vertex
What is the number of edges in a complete undirected graph with N vertices? N * (N-1) / 2
Weighted graph: a graph in which each edge carries a value back
Problem: Search for a certain node or traverse all nodes in the graph Depth First Search ◦ Once a possible path is found, continue the search until the end of the path Breadth First Search ◦ Start several paths at a time, and advance in each one step at a time back
Graph Representations Adjacency Matrix Adjacency Lists
Adjacency Matrix Let G=(V,E) be a graph with n vertices. The adjacency matrix of G is a two-dimensional n by n array, say adj_mat If the edge (vi, vj) is in E(G), adj_mat[i][j]=1 If there is no such edge in E(G), adj_mat[i][j]=0 The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric
G1G1 G2G2 G4G symmetric undirected: n 2 /2 directed: n 2 back
Adjacency Lists (data structures) Each row in adjacency matrix is represented as an adjacency list.
G1G G3G G4G An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes back
Thank You