180 likes | 666 Views
Graph Theory. c. b. d. a. e. Def: A graph is a set of vertices and edges G={V,E}. Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd}.
E N D
Graph Theory c b d a e Def: A graph is a set of vertices and edges G={V,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.
c b d a e c b d a Ex. Disconnected: e Def. 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)
Def. An Eulerian Path is a path that includes each edge exactly once. (history: Koenigsburg) b a c e d b a c An Eulerian path exists only in a graph with 0 or 2 odd vertices. e d Ex. abcaedc Ex.2 No Eulerian path exists.
b e a c d Def. 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
b a d e c In a dfs we “get as far away from start as fast as possible” Depth 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.
b a d e c Breadth 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.