Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.

Similar presentations


Presentation on theme: "Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices."— Presentation transcript:

1 Graphs CS-240/341

2 Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices (nodes) connected by a set of directed edges (arcs) –G = {V} {E} where V= {V 1,V 2,V 3 …V n } E= { (V m1,V n1 ),(V m2,V n2 )…(V mx,V ny ) } 1 2 45 3

3 Represent a relationship between two verticies –to ; in a digraph represents that A is adjacent to B –from ; B is adjacent from A Edges may also have an associated weight or value –if the vertices represent cities then the edge may represent a connection (road) between them and the weight may represent the distance and the direction may represent a one way street –if the graph represents a network of active components (p-n junctions, diodes/transistors) then the direction represents the direction of the pn junctions and the edges represent the way the components are connected and the weights represent the forward resistance of the junction (backward resistance is infinity) Edges AB 25

4 Digraph Abstract Data Type Data Elements: –collection of vertices and a collection of edges and weights Basic Operations: –Create an empty digraph –Check if the digraph is empty –Destroy a digraph –Insert a new vertex –Insert a new edge between two existing vertices –Delete a vertex and all directed edges to or from it –Delete a directed edge between to vertices –search for an edge value

5 Digraph representation Adjacency Matrix Representation 0 1 24 3 0 1 2 3 4 0 0 5 2 4 0 1 0 0 5 0 0 2 0 1 0 0 6 3 0 0 0 0 0 4 0 0 0 0 0 5 4 3 2 5 6 from to

6 Digraph Representation Adjacency List –use an array of pointers to be the heads of a list of adjacencies for each vertex 0 1 24 3 5 4 3 2 5 6 0123401234 1 52 33 4 2 5 4 6 1 2

7 Graph Traversal Depth First 1. Visit the start vertx V 2. For each vertex adjacent to V do the following: if w has not been visited, apply the depth-first search algorithm starting with w connecting the vertices along the way will produce a spanning tree 0 1 24 3 5 4 3 2 5 6 0123401234 1 52 33 4 2 5 4 6 1 2 0 1 24 3 5 4 3 2 5 6 V Spanning tree visited

8 Graph Traversal Breadth First 1. Visit the start vertex V 2 Initialize a queue to contain only the start vertex 3. While the queue is not empty do the following Rempve vertex V from the queue For all vertices w adjacent to v do the following: If w has not been visited then Visit W Add W to the queue

9 Spanning Trees The set of vertices and edges produced by a DFS traversal produces what is known as a spanning tree –if the DFS doesn’t include all of the vertices, then do another DFS starting at a vertex not yet in the tree. This may have to be done a number of times but will eventually produce a set disjoint (not connected) spanning trees called a spanning forest.

10 Degree of a vertex In degree is the number of edges coming into a vertex Out degree is the number of edges going out of a vertex 3 In degree = 3 Out degree = 2


Download ppt "Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices."

Similar presentations


Ads by Google