Title: Graph Theory
1Graph Theory
Def A graph is a set of vertices and edges
GV,E
Ex. V a,b,c,d,e E
ab,bd,ad,ed,ce,cd
Note above is a purely mathematical definition.
In computer science a graph is a data structure
where vertices (nodes) represent objects which in
turn represent real world data, and edges
represent references to objects how objects are
related.
2Def. A path is a list of consecutive edges.
Ex. abdecd
(length 5)
Def. The degree of a vertex is the no. of edges
incident of the vertex.
Ex. degree of d is 4, degree of a is 2
Def. A connected graph is a graph for which
there exists at least one path from any veretex
to any other vertex.
Ex. (above graph is connected)
3Def. An Eulerian Path is a path that includes
each edge exactly once. (history Koenigsburg)
Ex. abcaedc
Ex.2
No Eulerian path exists.
4Def. An adjacency matrix is a matrix used to
represent a graph.
Ex.
a b c d e a 0 1 0 0 1 b 1 0 0
0 1 c 0 0 0 1 1 d 0 0 1 0 1 e 1
1 1 1 0
Note 1 main diagonal is always zeros when there
are no loops in the graph
Note 2 symmetric w.r.t. main diagonal (always
true for an undirected graph).
Note 3 this is also called a Boolean Matrix
5Depth First Search
(traversal - may have other purpose than
searching)
Find all vertices that can be reached starting at
a given vertex.
Ex.
1. abde (push each vertex on stack)
2. since no unvisited vertex reachable from e,
pop e
3. same for d and b
4. Pop a, which has 1 unvisited vertex c
Therefore, the dfs from vertex a is a b d e c.
6Breadth First Search
Visit all vertices adjacent to starting vertex
and insert in a queue.
1. Choose start vertex a.
2. Insert in queue b, then c, (bc)
3. Remove b and repeat for all unvisited
adjacent vertices.
(cd)
4. Remove c, (no adj.vert.) (d)
bfs a b c d e
5. Remove d, add e (e)
6. Remove e (no adj.vert.)
7. Stop! Queue is empty.