Download presentation
Presentation is loading. Please wait.
Published byMillicent Edwards Modified over 9 years ago
1
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 10 - Graphs Original Slides by Rada Mihalcea
2
What is a Graph? A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V An edge e = (u,v) is a pair of vertices Example: a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)}
3
Applications electronic circuits networks (roads, flights, communications) CS16 LAX JFK LAX DFW STL HNL FTL
4
Directed vs. Undirected Graph An undirected graph is one in which the pair of vertices in a edge is unordered, (v 0, v 1 ) = (v 1,v 0 ) A directed graph is one in which each edge is a directed pair of vertices, != tail head
5
Terminology: Adjacent and Incident If (v 0, v 1 ) is an edge in an undirected graph, v 0 and v 1 are adjacent The edge (v 0, v 1 ) is incident on vertices v 0 and v 1 If is an edge in a directed graph v 0 is adjacent to v 1, and v 1 is adjacent from v 0 The edge is incident on v 0 and v 1
6
6 Terminology: Path path: sequence of vertices v 1,v 2,...v k such that consecutive vertices v i and v i+1 are adjacent. 3 3 3 3 2 a b c d e a b c d e a b e d cb e d c
7
More Terminology simple path: no repeated vertices cycle: simple path, except that the last vertex is the same as the first vertex a b c d e b e c
8
More… tree - connected graph without cycles forest - collection of trees
9
Graph Representations Adjacency Matrix Adjacency Lists
10
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 (v i, v j ) 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
11
Examples for Adjacency Matrix G1G1 G2G2 G4G4 0 12 3 0 1 2 1 0 2 3 4 5 6 7 symmetric undirected: n 2 /2 directed: n 2
12
Adjacency Lists (data structures) #define MAX_VERTICES 50 typedef struct node *node_pointer; typedef struct node { int vertex; struct node *link; }; node_pointer graph[MAX_VERTICES]; int n=0; /* vertices currently in use */ Each row in adjacency matrix is represented as an adjacency list.
13
01230123 012012 0123456701234567 1 23 023 013 0 12 G1G1 1 0 2 G3G3 1 2 0 3 0 3 12 5 4 6 57 6 G4G4 0 12 3 0 1 2 1 0 2 3 4 5 6 7 An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.