Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Theory 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.

Similar presentations


Presentation on theme: "Graph Theory 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."— Presentation transcript:

1

2 Graph Theory 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. b a c d e

3 b a c d 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) b a c d e Ex. Disconnected:

4 Def. An Eulerian Path is a path that includes each edge exactly once. (history: Koenigsburg) Ex. abcaedc a b c d e Ex.2 a b c d e No Eulerian path exists. An Eulerian path exists only in a graph with 0 or 2 odd vertices.

5 Def. An adjacency matrix is a matrix used to represent a graph. Ex. a b c d e 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

6 Depth First Search (traversal - may have other purpose than searching) Find all vertices that can be reached starting at a given vertex. Ex. a b c d e 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. In a dfs we “get as far away from start as fast as possible”

7 Breadth First Search Visit all vertices adjacent to starting vertex and insert in a queue. a b c d e 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) 5. Remove d, add e (e) 6. Remove e (no adj.vert.) 7. Stop! Queue is empty. bfs = a b c d e


Download ppt "Graph Theory 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."

Similar presentations


Ads by Google