120 likes | 268 Views
Vertices. Edges. Introduction Graphs . A graph G=(V,E) V =set of vertices (nodes) E =set of edges Examples : a map with cities connected by streets Cities are vertices Streets are edges Two basic types Undirected graphs (graphs) edge (u,v)=edge (v,u) Directed graphs (digraphs)
E N D
Vertices Edges IntroductionGraphs • A graph G=(V,E) • V=set of vertices (nodes) • E=set of edges • Examples: a map with cities connected by streets • Cities are vertices • Streets are edges • Two basic types • Undirected graphs (graphs) • edge (u,v)=edge (v,u) • Directed graphs (digraphs) • edge (u,v) goes from vertex u to vertex v • edge (u,v) is not the same as edge (v,u) • Examples: • Undirected graphs: two-way street • Directed graphs: one-way street
A A B B C C D D Directed graph: V = {A, B, C, D} E = {(A,B), (A,C), (A,D), (C,B)} Undirected graph: V = {A, B, C, D} E = {(A,B), (A,C), (A,D), (C,B), (B,A), (C,A), (D,A), (B,C)} • Weighted graph G = (V, E, W) • each edge e in the graph carries a value w(e) which is the weight or cost of e • Example: • edge weight = driving distance/time
d b a b d e c a e e c a b d c Vertex a is adjacent to c and vertex cis adjacent to a Vertex c is adjacent to a, but vertex a is NOT adjacent to c • Adjacent vertices: connected by an edge • Vertex v is adjacent to u if and only if (u, v) E. • In an undirected graph with edge (u, v), and hence (v, u), v is adjacent to u and u is adjacent to v. • A Loop • an edge (v, v) from a vertex to itself
a b d e c a b d e c Paths and cycles • A Path in a graph from w0 to wk is a sequence of edges between vertices w0, w1, …, wk, such that (wi, wi+1) E, for 0 i k • The length of the path is k, the number of edges on the path • A simple path no repeated vertices, except that the first and last could be the same. abedce is a path. cdeb is a path. bca is NOT a path. acde is a path. abec is NOT a path. abedc is a simple path. cdec is a simple path. abedce is NOT a simple path.
abeda is a simple cycle. abeceda is a cycle, but is NOT a simple cycle. abedc is NOT a cycle. a b d e c a b d e c aba is NOT a cycle. abedceda is NOT a cycle. abedcea is a cycle, but NOT simple. abea is a simple cycle. • Cycles • A cycle in a directed graph • A path of length at least 2 such that the first vertex on the path is the same as the last one • if the path is simple, then the cycle is a simple cycle. • A cycle in a undirected graph • A path of length at least 3 such that the first vertex on the path is the same as the last one. • No repeated edges
n = 4 Some special graphs • A complete graph is a graph with an edge between every pair of vertices. • A graph is called complete graph if every vertex is adjacent to every other vertex. • connected graph: any two vertices are connected by some path • connected component: connected subgraph.
Acyclic graph is a graph no cycles • DAG: directed acyclic graph • Tree: connected graph without cycles • Forest: collection of trees • We will express running times in terms of • |V| = number of vertices, and • |E| = number of edges
Graph representation:Adjacency matrixAdjacency list Adjacency matrix • Assume V = {1, 2, …, n} • An adjacency matrixrepresents the graph as a n n matrix A: • A[i, j] = 1 if edge(i, j) E (or weight of edge) = 0 if edge(i, j) E
Adjacency Matrix 1 2 3 4 5 2 1 0 1 0 1 0 3 2 1 0 0 0 1 1 3 0 0 0 0 1 4 4 1 0 0 0 1 5 5 0 1 1 1 0 • Diagonal entries are zero • Adjacency matrix of an undirected graph is symmetric • A(i, j) = A(j, i) for all i and j
Adjacency Matrix 1 2 3 4 5 2 1 0 0 0 1 0 3 2 1 0 0 0 1 1 3 0 0 0 0 0 4 4 0 0 0 0 1 5 5 0 1 1 0 0 • Diagonal entries are zero • Adjacency matrix of a digraph need not be symmetric
Graph representation: Adjacency list • Assume V = {1, 2, …, n} • Adjacency lists: for each vertex v V, store a list of vertices adjacent to v. • Adjacency list for vertex i is a linear list of vertices adjacent from vertex i • Each adjacency list is a chain.
Adjacency List 2 3 1 4 5 aList[1] 2 4 [2] 1 5 [3] 5 [4] 5 1 aList[5] 2 4 3 # of chain nodes = 2|E| (undirected graph) # of chain nodes = |E| (digraph)