Download presentation
Presentation is loading. Please wait.
1
Algorithms: Design and Analysis
, Semester 2, 7. Introduction to Graphs Objective introduce the main kinds of graphs, discuss two implementation approaches
2
1. Graphs Example: PVD ORD SFO LGA HNL LAX DFW MIA
A graph has two parts (V, E), where: V are the nodes, called vertices E are the links between vertices, called edges Example: airports and distance between them 849 PVD 1843 ORD 142 SFO 802 LGA 1743 337 1387 HNL 2555 1099 LAX 1233 DFW 1120 MIA
3
Graph Types not all graphs use weights on the edges Directed graph
the edges are directed e.g., bus cost network Undirected graph the edges are undirected e.g., road network not all graphs use weights on the edges
4
Graph Terminology X U V W Z Y a c b e d f g h i j
End vertices (or endpoints) of an edge U and V are the endpoints Edges incident on a vertex a, d, and b are incident Adjacent vertices U and V are adjacent Degree of a vertex X has degree 5 Parallel edges h and i are parallel edges Self-loop j is a self-loop X U V W Z Y a c b e d f g h i j
5
V a b P1 d U X Z P2 h c e W g f Y Path Simple path Examples
sequence of alternating vertices and edges begins with a vertex ends with a vertex each edge is preceded and followed by its endpoints Simple path path such that all its vertices and edges are distinct Examples P1=(V,b,X,h,Z) is a simple path P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple V a b P1 d U X Z P2 h c e W g f Y
6
V a b d U X Z C2 h e C1 c W g f Y Cycle Simple cycle Examples
circular sequence of alternating vertices and edges each edge is preceded and followed by its endpoints Simple cycle cycle such that all its vertices and edges are distinct Examples C1=(V,b,X,g,Y,f,W,c,U,a) is a simple cycle C2=(U,c,W,e,X,g,Y,f,W,d,V,a,) is a cycle that is not simple V a b d U X Z C2 h e C1 c W g f Y Graphs
7
Connectivity A graph is connected if there is a path between every pair of vertices Connected graph Non connected graph with two connected components
8
Strong Connectivity Each vertex can reach all other vertices a g c d e
b e f g
9
Directed Acyclic Graph (DAG)
A DAG has no cycles Some algorithms become simpler when used on DAGs instead of general graphs, based on the principle of topological ordering find shortest paths and longest paths by processing the vertices in a topological order
10
Bipartite Graph (bigraph)
A graph whose vertices can be divided into two disjoint sets U and V every edge connects a vertex in U to one in V
11
A Call Graph A call graph for a program: main makeList printList
mergeSort 4 examples of recursion split merge
12
Graphs are Everywhere Example Nodes Edges
Transportation network: airline routes airports nonstop flights Communication networks computers, hubs, routers physical wires Information network: web pages hyperlinks Information network: scientific papers articles references Social networks people “u is v’s friend”, “u sends to v”, “u’s FaceBook links to v” Computer programs functions (or modules) statement blocks “u calls v” “v can follow u”
13
Graph-related Problems
Search/traversal depth-first, breadth-first searches visit every node once (Euler) visit every edge once (Hamiltonian) Shortest paths Minimal Spanning Trees (MSTs) last year Network flow Matching Graph coloring Travelling salesman problem
14
2. Implementing Graphs We will typically express running times in terms of |E| and |V| (often dropping the |’s) If |E| |V|2 the graph is dense can also write this as |E| is O(|v2|) If |E| |V| the graph is sparse or |E| is O(|V|) Dense and sparse graphs are best implemented using two different data structures: Adjacency matricies: for dense graphs Adjacency lists: for sparse graphs
15
Dense Big-Oh In the most dense graph, a graph of v verticies will have |V|(|V|-1)/2 edges. In that case, for large n, |E| is O(|V|2) |V| = 5 |E| = (5*4)/2 = 10
16
Complete Graphs All pairs of vertices are connected by an edge.
No. of edges |E| = |V| (|V-1|)/2 = O(|V|2)
17
2.1. Adjacency Matrix a b a b c d e a 0 1 0 0 1 b 1 0 1 0 1 c c
b c c d d e e Graph Adjacency Matrix
18
Properties An adjacency matrix represents the graph as a V * V matrix A: A[i, j] = 1 if edge (i, j) E = 0 if edge (i, j) E The degree of a vertex v (of a simple graph) = sum of row v or sum of column v e.g. vertex a has degree 2 since it is connected to b and e An adjacency matrix can represent loops e.g. vertex c on the previous slide continued
19
The matrix duplicates information around the main diagonal
An adjacency matrix can represent parallel edges if non-negative integers are allowed as matrix entries ijth entry = no. of edges between vertex i and j The matrix duplicates information around the main diagonal the size can be easily reduced with some coding tricks Properties of graphs can be obtained using matrix operations e.g. the no. of paths of a given length, and vertex degree
20
The No. of Paths of Length n
If an adjacency matrix A is multiplied by itself repeatedly: A, A2, A3, ..., An Then the ijth entry in matrix An is equal to the number of paths from i to j of length n.
21
Example A = a b a b c d e a 0 1 0 1 0 b 1 0 1 0 1 c c 0 1 0 1 1 d
b c c d d e e
22
a b c d e a b A2 = = c d e
23
Why it Works... = 0*0 + 1*1 + 0*0 + 1*1 + 0*1 = 2
Consider row a, column c in A2: c b d a-b-c a-d-c a ( ) 1 b = 0*0 + 1*1 + 0*0 + 1*1 + 0*1 = 2 d 1 1 continued
24
A non-zero product means there is at least one vertex connecting verticies a and c.
The sum is 2 because of: (a, b, c) and (a, d, c) 2 paths of length two
25
The Degree of Verticies
The entries on the main diagonal of A2 give the degrees of the verticies (when A is a simple graph). Consider vertex c: degree of c == 3 since it is connected to the edges (c,b), (c,d), and (c,e). continued
26
In A2 these become paths of length 2:
(c,b,c), (c,d,c), and (c,e,c) So the number of paths of length 2 for c = the degree of c this is true for all verticies
27
Coding Adjacency Matricies
#define NUMNODES n int arcs[NUMNODES][NUMNODES]; arcs[u][v] == 1 if there is an edge (u,v); 0 otherwise Storage used: O(|V|2) The implementation may also need a way to map node names (strings) to array indicies. continued
28
If n is large then the array will be very large, with almost half of it being unnecessary.
If the nodes are lightly connected then most of the array will contain 0’s, which is a further waste of memory.
29
Representing Directed Graphs
A directed graph: 1 3 2 4
30
Its Adjacency Matrix finish Not symmetric; all the array may be necessary. Still a waste of space if nodes are lightly connected. 1 start 2 3 4
31
When to use an Adjacency Matrix
The adjacency matrix is an efficient way to store dense graphs. But most large interesting graphs are sparse e.g., planar graphs, in which no edges cross, have |e| = O(|v|) by Euler’s formula For this reason the adjacency list is often a better respresentation than the adjacency matrix
32
2.2. Adjacency List Adjacency list: for each vertex v V, store a list of vertices adjacent to v Example: adj[0] = {0, 1, 2} adj[1] = {3} adj[2] = {0, 1, 4} adj[3] = {2, 4} adj[4] = {1} Can be used for directed and undirected graphs. 1 3 2 4
33
An implementation diagram:
adj[] 1 2 1 3 2 1 4 3 2 4 means NULL size of array = no. of vertices (|V|) 4 1 no. of cells == no. of edges (|E|)
34
Data Structures struct cell { /* for a linked list */ Node nodeName; struct cell *next; }; struct cell *adj[NUMNODES]; adj[u] points to a linked list of cells which give the names of the nodes connected to u.
35
Storage Needs How much storage is required?
The degree of a vertex v == number of incident edges directed graphs have in-degree, out-degree values For directed graphs, the number of items in an adjacency lists is out-degree(v) = |E| This uses O(V + E) storage
36
This also uses O(V + E) storage
For undirected graphs, the number of items in the adjency list is degree(v) = 2*|E| (the handshaking lemma) Why? If we mark every edge connected to every vertex, then by the end, every edge will be marked twice This also uses O(V + E) storage In summary, adjacency lists use O(V+E) storage
37
2.3. Running Time: Matrix or List?
Which representation is better for graphs? The simple answer: dense graph – use a matrix sparse graph – use an adjcency list But a more accurate answer depends on the operations that will be applied to the graph. We will consider three operations: is there an edge between u and v? find the successors of u (in a directed graph) find the predecessors of u (in a directed graph) continued
38
Is there an edge (u,v)? Adjacency matrix: O(1) to read arcs[u][v]
Adjacency list: O(1 + E/V) // forget the |...| O(1) to get to adj[u] length of linked list is on average E/V if a sparse graph (E<<V): O(1+ E/V) => O(1) if a dense graph (E ≈ V2): O(1+ E/V) => O(V)
39
Find u’s successors (u->v)
Adjacency matrix: O(V) since must examine the entire row for vertex u Adjacency list: O(1 + (E/V)) since must look at entire list pointed to by adj[u] if a sparse graph (E<<V): O(1+ E/V) => O(1) if a dense graph (E ≈ V2): O(1+ E/V) => O(V)
40
Find u’s predecessors (t->u)
Adjacency matrix: O(V) since must examine the entire column for vertex u a 1 in the row for ‘t’ means that ‘t’ is a predecessor Adjacency list: O(E) since must examine every list pointed to by adj[] if a sparse graph (E<<V): O(E) is fast if a dense graph (E ≈ V2): O(E) is slow
41
Summary: which is faster?
Operation Dense Graph Sparse Graph Find edge Adj. Matrix Either Find succ. Either Adj. list Find pred. Adj. Matrix Either As a graph gets denser, an adjacency matrix has better execution time than an adjacency list.
42
2.4. Storage Space: Matrix or List?
The size of an adjacency matrix for a graph of V nodes is: V2 bits (assuming 0 and 1 are stored as bits) continued
43
An adjacency list cell uses:
32 bits for the integer, 32 bits for the pointer so, cell size = 64 bits Total no. of cells = total no. of edges, e so, total size of lists = 64*E bits successors[] has V entries (for V verticies) so, array size is 32*V bits Total size of an adjacency list data struct: 64*E + 32*V
44
Size Comparison An adjacency list will use less storage than an adjacency matrix when: 64*E + 32*V < V2 which is: E < V2/64 – V/2 When V is large, ignore the V/2 term: E < V2/64 continued
45
V2 is (roughly) the maximum number of edges.
So if the actual number of edges in a graph is 1/64 of the maximum number of edges, then an adj. list representation will be smaller than an adj. matrix coding but the graph must be quite sparse
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.