Introduction to Graph A graph consists of a set of vertices, and a set of edges that link together the vertices. A graph can be: Directed: Edges are directed. Undirected: Edges are undirected Data Structure and Algorithm
Undirected Graph A graph G = (V, E) V: vertices E : edges, unordered pairs of vertices from V V (u,v) is same as (v,u) Thus |E| <= |V| (|V|-1)/2 A O N M L K J E F G H D C B I P Data Structure and Algorithm
Directed Graph A graph G = (V, E) V: vertices E : edges, ordered pairs of vertices from VV (u,v) is different from (v,u) Thus |E| <= |V| (|V|-1) A E F G H D C B I Data Structure and Algorithm
Graph in Application Internet: web graphs Each page is a vertex Each edge represent a hyperlink Directed GPS: highway maps Each city is a vertex Each freeway segment is an undirected edge Undirected Data Structure and Algorithm
Using Adjacency Matrix Using Adjacency List Representing Graphs Using Adjacency Matrix Using Adjacency List Data Structure and Algorithm
Adjacency Matrix Representation Example: A 1 2 3 4 1 2 4 3 a d b c An adjacency matrix represents the graph as a n x n matrix A: A[i, j] = 1 if edge (i, j) E (or weight of edge) = 0 if edge (i, j) E Space Complexity: O(V2) Data Structure and Algorithm
Adjacency List Representation 1 2 3 4 2 3 1 2 4 3 a d b c 3 3 Space Complexity: O(V+E) Data Structure and Algorithm