Title: Graph
1Graph
- C and Data Structures
- Baojian Hua
- bjhua_at_ustc.edu.cn
2Whats a Graph?
- Graph a group of vertices connected by edges
3Why Study Graphs?
- Interesting broadly used abstraction
- not only in computer science
- challenge branch in discrete math
- Ex the 4-color problem
- hundreds of known algorithms
- with more to study
- numerous applications
4Broad Applications
Graph Vertices Edges
communication telephone cables
software functions calls
internet web page hyper-links
social relationship people friendship
transportation cities roads
5Graph Terminology
1
2
3
4
6
5
A sample graph taken from chapter 22 of
Introduction to Algorithms.
6Graph Terminology
7Graph ADT
- A graph is a tuple (V, E)
- V is a set of vertices v1, v2, , vn
- E is a set of edges ltvi, vjgt (1lti, jltn)
- Typical operations
- graph creation
- search a vertex (or an edge)
- traverse all vertexes
8Example
G (V, E) V 1, 2, 3, 4, 5, 6 E (1,
2), (2, 5), (3, 5), (3,
6), (4, 1), (4, 2), (5,
4), (6, 6)
9Representation
- Two popular strategies
- array-based (adjacency matrix)
- Keep an extensible two-dimensional array M
internally - Mij holds the edge ltvi, vjgt, if there exists
one - linear list-based (adjacency list)
- for every vertex vi, maintain a linear list
listltvigt - listltvigt stores vis out-going edges
10Adjacency Matrix
0
5
1
2
3
4
0
1
2
Note the mapping function map (n) n-1
3
4
5
11Adjacency List
1
1-gt2
2
2-gt5
3
3-gt5
3-gt6
4
4-gt1
4-gt2
5
5-gt4
6
6-gt6
12graph ADT Interface
- // This slides assume all graphs directed.
- // Undirected ones are similar and easier.
- // In file graph.h
- ifndef GRAPH_H
- define GRAPH_H
- define T Graph_t
- typedef struct T T
- T Graph_new ()
- void Graph_insertVertex (T g, poly data)
- void Graph_insertEdge (T g, poly from, poly to)
- // more to come later
- undef T
- endif
13Client Code
graph g newGraph () insertVertex (g,
1) insertVertex (g, 2) insertVertex (g,
6) insertEdge (g, 1, 2) insertEdge (g, 2,
5) insertEdge (g, 6, 6)
14Graph Implementation 1 Adjacency Matrix
- // adjacency matrix-based implementation
- include matrix.h
- include dict.h
- include graph.h
- struct Graph_t
-
- Matrix_t m
- // remember the index
- Dict_t d
0
2
3
1
0
1
2
3
15Matrix Interface
- // file matrix.h
- ifndef MATRIX_H
- define MATRIX_H
- define T Matrix_t
- typedef struct T T
- T Matrix_new ()
- void Matrix_assign (matrix m, int i, int j)
- int Matrix_getNextIndex (matrix m)
- endif
- // Implementation may make use of a two-
- // dimensional extensible array, leave to you.
16Adjacency Matrix-based Graph Creation
- T Graph_new ()
-
- T g malloc (sizeof (g))
- g-gtm Matrix_new () // an empty matrix
- g-gtd Dict_new () // an empty dictionary
- return g
-
17Adjacency Matrix-based Inserting Vertices
- void Graph_insertVertex (T g, poly data)
-
- int i Matrix_getNextIndex (g-gtmatrix)
- Dict_insert (g-gtdict, data, i)
- return
-
0
1
2
3
4
0
2
3
0
1
0
1
1
2
2
3
3
4
18Graph Implementation 1 Inserting Edges
- void Graph_insertEdge (T g, poly from, poly to)
-
- int f Dict_Lookup (g-gtdict, from)
- int t Dict_Lookup (g-gtdict, to)
- Matrix_assign (g-gtmatrix, f, t)
- return
-
0
1
2
3
4
0
2
3
0
1
0
1
1
2
2
3
3
4
19Summary
- Pros
- Relatively easy to implement
- But need another level of indirection from data
to array indexes - Searching vertices or edges can be fast
- May be too space-consuming
- many empty slots in matrix it the graph has many
vertices but few edges
20Graph Representation 2 Adjacency List
struct T List_Tv vertices struct Tv
poly data List_Te edges struct Te Tv
from Tv to
- include linked-list.h
- include graph.h
- define T Graph_t
- define Tv Vertex_t
- define Te Edge_t
- define List_Tv \ LinkedList_t
- define List_Te \
- LinkedList_t
- typedef struct Tv Tv
- typedef struct Te Te
21Graph Representation 2 Adjacency List
- struct T
- List_Tv vertices
-
- struct Tv
- poly data
- List_Te edges
-
- struct Te
- Tv from
- Tv to
22Adjacency List-based Graph Creation
- // Convention for colors
- // graph, linkedList, data, vertex, edge
- T Graph_new ()
-
- T g malloc (sizeof (g))
- g-gtvertices LinkedList_new ()
- return g
-
23Adjacency List-basedCreating New Vertex
- // Convention for colors
- // graph, linkedList, data, vertex, edge
- Tv Vertex_new (poly data)
-
- Tv v malloc (sizeof (v))
- v-gtdata data
- v-gtedges LinkedList_new ()
- return v
-
24Adjacency List-basedCreating New Edge
- // Convention for colors
- // graph, linkedList, data, vertex, edge
- Te Edge_new (Tv from, Tv to)
-
- Te e malloc (sizeof (e))
- e-gtfrom from
- e-gtto to
-
- return e
-
25Adjacency List-basedInserting New Vertex
- void Graph_insertVertex (T g, poly data)
-
- Tv v Vertex_new (data)
- LinkedList_insertTail (g-gtvertices, v)
- return
26Adjacency List-basedInserting New Edge
- void Graph_insertEdge (T g, poly from, poly to)
-
- Tv vf lookupVertex (g, from)
- Tv vt lookupVertex (g, to)
- Te e Edge_new (vf, vt)
- LinkedList_insertTail (vf-gtedges, e)
- return
-
- // insert 0-gt4
27Adjacency List-basedInserting New Edge
- void Graph_insertEdge (T g, poly from, poly to)
-
- Tv vf lookupVertex (g, from)
- Tv vt lookupVertex (g, to)
- Te e Edge_new (vf, vt)
- LinkedList_insertTail (vf-gtedges, e)
- return
28Adjacency List-basedInserting New Edge
- void Graph_insertEdge (T g, poly from, poly to)
-
- Tv vf lookupVertex (g, from)
- Tv vt lookupVertex (g, to)
- Te e Edge_new (vf, vt)
- LinkedList_insertTail (vf-gtedges, e)
- return
29Adjacency List-basedInserting New Edge
- void insertEdge (graph g, poly from, poly to)
-
- Tv vf lookupVertex (g, from)
- Tv vt lookupVertex (g, to)
- Te e Edge_new (vf, vt)
- LinkedList_insertTail (vf-gtedges, e)
- return
0
0-gt1
0-gt2
0-gt3
0-gt4
1
2
3
4
30Adjacency List-basedInserting New Edge
- void insertEdge (T g, poly from, poly to)
-
- Tv vf lookupVertex (g, from)
- Tv vt lookupVertex (g, to)
- Te e Edge_new (vf, vt)
- LinkedList_insertTail (vf-gtedges, e)
- return
0
0-gt1
0-gt2
0-gt3
0-gt4
1
2
3
4
31Example
32Client Code for This ExampleStep 1 Cook Data
- T g Graph_new ()
- Int_t n1 Int_new (1)
- Int_t n2 Int_new (2)
- Int_t n3 Int_new (3)
- Int_t n4 Int_new (4)
- Int_t n5 Int_new (5)
- Int_t n6 Int_new (6)
2
3
1
4
6
5
33Client Code ContinuedStep 2 Insert Vertices
- Graph_insertVertex (g, n1)
- Graph_insertVertex (g, n2)
- Graph_insertVertex (g, n3)
- Graph_insertVertex (g, n4)
- Graph_insertVertex (g, n5)
- Graph_insertVertex (g, n6)
2
3
1
1
2
3
4
6
5
4
6
5
34Client Code ContinuedStep 3 Insert Edges
- Graph_insertEdge (g, n1, n2)
- Graph_insertEdge (g, n2, n5)
- Graph_insertEdge (g, n3, n5)
- Graph_insertEdge (g, n3, n6)
- Graph_insertEdge (g, n4, n1)
- Graph_insertEdge (g, n4, n2)
- Graph_insertEdge (g, n5, n4)
- Graph_insertEdge (g, n6, n6)
- // Done! -)
1
2
3
4
6
5
35All in PictureAn Empty Graph
- // Ill make use of this convention for colors
- // graph, linkedList, data, vertex, edge
g
next
data
36All in PictureAfter Inserting all Vertices
- // Ill make use of this convention for colors
- // graph, linkedList, data, vertex, edge
g
next
/\
data
1
6
5
2
3
4
data
next
/\
/\
/\
/\
/\
37All in PictureAfter Inserting all Edges (Part)
- // Ill make use of this convention for colors
- // graph, linkedList, data, vertex, edge
g
next
/\
data
1
6
5
2
3
4
data
next
/\
/\
/\
/\
from
from
to
to
/\
/\