1 / 9

Definitions

Definitions. Graph: Collection of nodes and edges Adjacent: Two nodes are adjacent if they are connected by a single edge Path: A sequence of adjacent vertices Non-directed: Edge (v,w) implies (w,v) Directed: Edge (v,w) doesn’t imply (w,v)

kerem
Download Presentation

Definitions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Definitions • Graph: Collection of nodes and edges • Adjacent: Two nodes are adjacent if they are connected by a single edge • Path: A sequence of adjacent vertices • Non-directed: Edge (v,w) implies (w,v) • Directed: Edge (v,w) doesn’t imply (w,v) • Weighted: Edges have weights associated with them. • Connected: There is at least one path from any vertex to any other vertex.

  2. Representing Vertices • Adjacency Matrix typedef struct Graph { Vertex *vertexList; EDGE adjMat[V][V]; } • Adjacency Listtypedef struct Graph{ VERTEX *vertexList; }// Note: For adjacency lists, each vertex contains a linked list of adjacent edges • Sample Nodes (differs depending on the representation) typedef struct VERTEX_LNK { int vertex; int color; int weight; struct *EDGE_LNK edges; } VERTEX typedef struct EDGE_LNK { char *label; int weight; struct Edge_LNK *ptr;} EDGE

  3. Adjacency Matrix • Two dimension array of rows and columns • Works well for an a graph that is dense • For graphs that are sparse, lots of overhead is incurred skipping over null edges A C 2 4 5 3 D 7 B Dense graph: One in which has more than half of its possible edges declared

  4. Adjacency Lists B,3 • One dimension array of linked lists • Works well for an a graph that is sparse • For graphs that are dense, lots of overhead is incurred because we loose direct access capability • For dense graphs, more C,4 A,2 C,5 D,7 A C 2 4 5 3 D 7 B

  5. Parallel Arrays • Parallel arrays has the advantage of making a good use of memory for both sparse and dense graphs, while preserving the direct access of edges • Parallel arrays make it easy to send graphs between computers A C 2 4 5 3 D 7 B

  6. Graph Algorithms • Traversal • Depth First For all Vertices, clear colors depthFirstSearch(Vertex v) If not null Then Visit() and color the Vertex push all unvisited adjacent vertices onto the stackWhile stack not empty pop a vertex, newVertex, from the stack depthFirstSearch(newVertex) • Breadth First (Same as depth first except use a queue) • Complexity is O(E+V)

  7. Minimum Spanning TreeMinimum edge set of a graph that visits all edges |T|=|V|-1 • Undirected –DFS or BFS; record vertices and unvisited neighbors • Directed – Algorithm 1 (Kruskal’s Algorithm) Sort the edges (O(E lg E) For each of the sorted edges {(O(~E) with Union Find Algorithm} If a cycle is not created, add the edge to the spanning tree • Directed – Algorithm 2 (Prim’s Algorithm) Create a priority queue (StartV Key=0, OtherVertices Key = infinity) While priority queue is not empty min = RemoveMin()For each vertex adj adjacent to minIf weight(min,adj)< key(adj) Then adjustKey(adj) and set Predecessor[adj] = min • Complexities O(ElgE) Kruskal and O(lgV*(V+E))Prim

  8. Topological SortCritical Path Analysis • Application, sequence of courses where prerequisites are met • Limitations: Graph cannot have cycles (v1,v2,…,vk,v1) • Algorithm Let G=[V,E] be a clone of the original graph WhileV > 0 vertex = FindVertexWithNoSuccessors() IffoundThenCall delete(vertex) sortedArray[--V] = vertex Else Print error message; graph has cycles

  9. Shortest PathRequirements: Directed Graph with positive weights • Step 1:Create a shortest path array, short, with each entry having an infinite weight Except the original vertex that has a weight of zero. • Step 2:Create a path array, path, that will indicate the shortest path from one vertex to another • Step 3: The Algorithm While more vertices are left Find the remaining vertex, light, with the lightest weight For each edge, (light,w), of light If edge weight w(light,w)+short(light) < short(light) Thenshort[light] = w(light,w) + short(light) path[light] = w • Complexity: O(V+lg V*V+lgV*E) = O(lgV*(V+E)) if a heap is used to order, remove-min, and relax weights.

More Related