chapter 9 graphs some concepts a graph consists of a set
play

Chapter 9 Graphs Some concepts A graph consists of a set of - PowerPoint PPT Presentation

CS 2412 Data Structures Chapter 9 Graphs Some concepts A graph consists of a set of vertices and a set of lines (edges, arcs). A directed graph, or digraph, is a graph in which each line has a direction. Usually, the lines are called


  1. CS 2412 Data Structures Chapter 9 Graphs

  2. Some concepts • A graph consists of a set of vertices and a set of lines (edges, arcs). • A directed graph, or digraph, is a graph in which each line has a direction. Usually, the lines are called arcs. • A path is a sequence of vertices in which each vertex is adjacent to the next one, where adjacent means that there is an line from one to the next one. • A cycle is a path consisting of at least three vertices that starts and ends with the same vertex. • A loop is a single arc from a vertex to itself. • A connected graph is a graph in which any pair of vertices has a path between them. Data Structure 2016 R. Wei 2

  3. For a directed graph: • Strong connected if there is a path from each vertex to every other vertex. • Weakly connected if there is a path from each vertex to every other vertex when the direction of the arc is ignored, but at least two vertices are not connected when we consider the direction of the path. • The outdegree of a vertex is the number of arcs leaving the vertex. • The indegree is the number of arcs entering the vertex. Data Structure 2016 R. Wei 3

  4. Data Structure 2016 R. Wei 4

  5. Data Structure 2016 R. Wei 5

  6. Data Structure 2016 R. Wei 6

  7. Operations • Inset a vertex: add a new vertex to the graph. • Delete a vertex: removed a vertex from the graph and all edges connected to that vertex are also removed. • Add an edge: two vertices must be specified to add an edge. For a digraph, one vertex must be specified as the source and other one as the destination. • Delete an edge: remove an edge from the graph. Data Structure 2016 R. Wei 7

  8. Traverse graph: • Depth-first traversal: Suppose the traversal has just visited a vertex v which is adjacent to vertices w 1 , w 2 , · · · , w k . Then the traversal visit w 1 and traverse all the vertices to which it is adjacent before returning to traverse w 2 , · · · , w k . • Breadth-first traversal: Suppose the traversal has just visited a vertex v . Then it next visits all the vertices adjacent to v . Data Structure 2016 R. Wei 8

  9. We can use a recursive method to implement graph traverse. But we also can use a stack to implement. Depth-first traversal: 1. Push the first vertex into the stack. 2. Loop: pop a vertex from the stack process the vertex push all of the adjacent vertices into the stack. 3. When the stack is empty, the traversal is complete. Data Structure 2016 R. Wei 9

  10. Data Structure 2016 R. Wei 10

  11. Use a queue to implement the breadth-first traversal: 1. Enqueue the first vertex into the queue. 2. Loop: dequeue a vertex from the Process the vertex. place all of its adjacent vertices into the queue. 3. When the queue is empty, the traversal is complete. Data Structure 2016 R. Wei 11

  12. Data Structure 2016 R. Wei 12

  13. Graph representation • Adjacency matrix: a 0-1 matrix its rows and columns are indexed by vertices. The cell i, j is 1 if and only if the i th vertex and the j th vertex is adjacent. The matrix of a non-directed graph must be symmetric to its main diagonal. • Adjacency list: a list of vertices that are adjacent to the vertex. Each vertex has a adjacent list (unless it is isolated). Data Structure 2016 R. Wei 13

  14. Data Structure 2016 R. Wei 14

  15. There are different methods to implement graphs. The following implementation are based on the adjacent list. Data Structure 2016 R. Wei 15

  16. Data Structure 2016 R. Wei 16

  17. typedef struct { int count; struct vertex* first; int (*compare) (void* argu1, void* argu2); } GRAPH; typedef struct vertex { struct vertex* pNextVertex; void* dataPtr; int inDegree; int outDegree; short processed; struct arc* pArc; } VERTEX; typedef struct arc { struct vertex* destination; struct arc* pNextArc; } ARC; Data Structure 2016 R. Wei 17

  18. To create a graph is simple. Data Structure 2016 R. Wei 18

  19. GRAPH* graphCreate (int (*compare) (void* argu1, void* argu2)) { GRAPH* graph; graph = (GRAPH*) malloc (sizeof (GRAPH)); if (graph) { graph->first = NULL; graph->count = 0; graph->compare = compare; } // if return graph; } // graphCreate Data Structure 2016 R. Wei 19

  20. For the insertion and deletion of vertex in a graph with adjacency list implementation: • The vertex list is arranged in order (usually in the increasing order corresponding to the keys). • For insertion, first insert the vertex to the vertex list according to the order of key. Then insert the related edges. • For deletion, first delete the edges connected to the vertex that will be deleted. Then delete the vertex. • We only consider the graph here. But the implementation of digraph is similar. Data Structure 2016 R. Wei 20

  21. Data Structure 2016 R. Wei 21

  22. void graphInsVrtx (GRAPH* graph, void* dataInPtr) { VERTEX* newPtr; VERTEX* locPtr; VERTEX* predPtr; newPtr = (VERTEX*)malloc(sizeof (VERTEX)); if (newPtr) { newPtr->pNextVertex = NULL; newPtr->dataPtr = dataInPtr; newPtr->inDegree = 0; newPtr->outDegree = 0; newPtr->processed = 0; newPtr->pArc = NULL; (graph->count)++; } // if malloc else printf("Overflow error 100\a\n"), exit (100); locPtr = graph->first;// Now find insertion point if (!locPtr) Data Structure 2016 R. Wei 22

  23. graph->first = newPtr; else { predPtr = NULL; while (locPtr && (graph->compare (dataInPtr, locPtr->dataPtr) > 0)) { predPtr = locPtr; locPtr = locPtr->pNextVertex; } // while if (!predPtr) graph->first = newPtr; else predPtr->pNextVertex = newPtr; newPtr->pNextVertex = locPtr; } // else return; } // graphInsVrtx Data Structure 2016 R. Wei 23

  24. Data Structure 2016 R. Wei 24

  25. int graphDltVrtx (GRAPH* graph, void* dltKey) { VERTEX* predPtr; VERTEX* walkPtr; if (!graph->first) return -2; predPtr = NULL; walkPtr = graph->first; while (walkPtr && (graph->compare(dltKey, walkPtr->dataPtr) > 0)) { predPtr = walkPtr; walkPtr = walkPtr->pNextVertex; } // walkPtr && if (!walkPtr || graph->compare(dltKey, walkPtr->dataPtr) != 0) Data Structure 2016 R. Wei 25

  26. return -2; // then found vertex. Test degree if ((walkPtr->inDegree > 0) || (walkPtr->outDegree > 0)) return -1; if (!predPtr) //delete graph->first = walkPtr->pNextVertex; else predPtr->pNextVertex = walkPtr->pNextVertex; --graph->count; free(walkPtr); return 1; } // graphDltVrtx Data Structure 2016 R. Wei 26

  27. Data Structure 2016 R. Wei 27

  28. Data Structure 2016 R. Wei 28

  29. int graphInsArc (GRAPH* graph, void* pFromKey, void* pToKey) { ARC* newPtr; ARC* arcPredPtr; ARC* arcWalkPtr; VERTEX* vertFromPtr; VERTEX* vertToPtr; newPtr = (ARC*)malloc(sizeof(ARC)); if (!newPtr) return (-1); //locate source vertex vertFromPtr = graph->first; while (vertFromPtr && (graph->compare(pFromKey, vertFromPtr->dataPtr) > 0)) { vertFromPtr = vertFromPtr->pNextVertex; } if (!vertFromPtr || (graph->compare(pFromKey, vertFromPtr->dataPtr) != 0)) Data Structure 2016 R. Wei 29

  30. return (-2); vertToPtr = graph->first; while (vertToPtr && graph->compare(pToKey, vertToPtr->dataPtr) > 0) { vertToPtr = vertToPtr->pNextVertex; } if (!vertToPtr || (graph->compare(pToKey, vertToPtr->dataPtr) != 0)) return (-3); // From and to vertices located. Insert new arc ++vertFromPtr->outDegree; ++vertToPtr ->inDegree; newPtr->destination = vertToPtr; if (!vertFromPtr->pArc) { // Inserting first arc for this vertex vertFromPtr->pArc = newPtr; newPtr-> pNextArc = NULL; return 1; Data Structure 2016 R. Wei 30

  31. } // Find insertion point in adjacency (arc) list arcPredPtr = NULL; arcWalkPtr = vertFromPtr->pArc; while (arcWalkPtr && graph->compare(pToKey, arcWalkPtr->destination->dataPtr) >= 0) { arcPredPtr = arcWalkPtr; arcWalkPtr = arcWalkPtr->pNextArc; } // arcWalkPtr && if (!arcPredPtr) // Insertion before first arc vertFromPtr->pArc = newPtr; else arcPredPtr->pNextArc = newPtr; newPtr->pNextArc = arcWalkPtr; return 1; } // graphInsArc Data Structure 2016 R. Wei 31

  32. Data Structure 2016 R. Wei 32

  33. Data Structure 2016 R. Wei 33

  34. int graphDltArc (GRAPH* graph, void* fromKey, void* toKey) { VERTEX* fromVertexPtr; VERTEX* toVertexPtr; ARC* preArcPtr; ARC* arcWalkPtr; if (!graph->first) return -2; // Locate source vertex fromVertexPtr = graph->first; while (fromVertexPtr && (graph->compare(fromKey, fromVertexPtr->dataPtr) > 0)) fromVertexPtr = fromVertexPtr->pNextVertex; if (!fromVertexPtr || graph->compare(fromKey, fromVertexPtr->dataPtr) != 0) return -2; // Locate destination vertex in adjacency list if (!fromVertexPtr->pArc) return -3; preArcPtr = NULL; arcWalkPtr = fromVertexPtr->pArc; while (arcWalkPtr && (graph->compare(toKey, Data Structure 2016 R. Wei 34

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend