CS 2412 Data Structures. Chapter 9 Graphs
|
|
- Susanna Bennett
- 6 years ago
- Views:
Transcription
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 ith vertex and the jth 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
35 arcwalkptr->destination->dataptr) > 0)) prearcptr = arcwalkptr; arcwalkptr = arcwalkptr->pnextarc; } // while arcwalkptr && if (!arcwalkptr (graph->compare(tokey, arcwalkptr->destination->dataptr)!= 0)) return -3; tovertexptr = arcwalkptr->destination; // from, tovertex & arcptr located. Delete arc --fromvertexptr->outdegree; --tovertexptr -> indegree; if (!prearcptr) // Deleting first arc fromvertexptr->parc = arcwalkptr->pnextarc; else prearcptr->pnextarc = arcwalkptr->pnextarc; free (arcwalkptr); return 1; } // graphdltarc Data Structure 2016 R. Wei 35
36 Data Structure 2016 R. Wei 36
37 int graphretrvrtx (GRAPH* graph, void* keyptr, void** pdataout) VERTEX* walkptr; if (!graph->first) return -2; walkptr = graph->first; while (walkptr && (graph->compare (keyptr, walkptr->dataptr) > 0)) walkptr = walkptr->pnextvertex; if (graph->compare(keyptr, walkptr->dataptr) == 0) *pdataout = walkptr->dataptr; return 1; } // if else return -2; } // graphretrvrtx Data Structure 2016 R. Wei 37
38 Data Structure 2016 R. Wei 38
39 Data Structure 2016 R. Wei 39
40 void graphdpthfrst (GRAPH* graph, void (*process) (void* dataptr)) bool success; VERTEX* walkptr; VERTEX* vertexptr; VERTEX* verttoptr; STACK * stack; ARC* arcwalkptr; if (!graph->first) return; // Set processed flags to not processed walkptr = graph->first; while (walkptr) walkptr->processed = 0; walkptr = walkptr->pnextvertex; } // while // Process each vertex in list stack = createstack (); walkptr = graph->first; Data Structure 2016 R. Wei 40
41 while (walkptr) if (walkptr->processed < 2) if (walkptr->processed < 1) // Push & set flag to pushed success = pushstack (stack, walkptr); if (!success) printf("\astack overflow 100\a\n"), exit (100); walkptr->processed = 1; } // if processed < 1 } // if processed < 2 // Process descendents of vertex at stack top while (!emptystack (stack)) vertexptr = popstack(stack); process (vertexptr->dataptr); vertexptr->processed = 2; // Push all vertices from adjacency list Data Structure 2016 R. Wei 41
42 arcwalkptr = vertexptr->parc; while (arcwalkptr) verttoptr = arcwalkptr->destination; if (verttoptr->processed == 0) success = pushstack(stack, verttoptr); if (!success) printf("\astack overflow 101\a\n"), exit (101); verttoptr->processed = 1; } // if verttoptr arcwalkptr = arcwalkptr->pnextarc; } // while pwalkarc } // while!emptystack walkptr = walkptr->pnextvertex; } // while walkptr destroystack(stack); return; } // graphdpthfrst Data Structure 2016 R. Wei 42
43 void graphbrdthfrst (GRAPH* graph, void (*process) (void* dataptr)) bool success; VERTEX* walkptr; VERTEX* vertexptr; VERTEX* verttoptr; QUEUE* queue; ARC* arcwalkptr; if (!graph->first) return; // Set processed flags to not processed walkptr = graph->first; while (walkptr) walkptr->processed = 0; walkptr = walkptr->pnextvertex; } // while // Process each vertex in list queue = createqueue (); walkptr = graph->first; Data Structure 2016 R. Wei 43
44 while (walkptr) if (walkptr->processed < 2) if (walkptr->processed < 1) // Enqueue & set flag to queue success = enqueue(queue, walkptr); if (!success) printf("\aqueue overflow 100\a\n"), exit (100); walkptr->processed = 1; } // if processed < 1 } // if processed < 2 // Process descendents of vertex at que frnt while (!emptyqueue (queue)) dequeue(queue, (void**)&vertexptr); process (vertexptr->dataptr); vertexptr->processed = 2; // Enqueue vertices from adjacency list Data Structure 2016 R. Wei 44
45 arcwalkptr = vertexptr->parc; while (arcwalkptr) verttoptr = arcwalkptr->destination; if (verttoptr->processed == 0) success = enqueue(queue, verttoptr); if (!success) printf("\aqueue overflow 101\a\n"), exit (101); verttoptr->processed = 1; } // if verttoptr arcwalkptr = arcwalkptr->pnextarc; } // while pwalkarc } // while!emptyqueue walkptr = walkptr->pnextvertex; } // while walkptr destroyqueue(queue); return; } // graphbrdthfrst Data Structure 2016 R. Wei 45
46 Data Structure 2016 R. Wei 46
47 Weighted graph Weighted graph is a graph whose edges are weighted. The meaning of the weights depends on the applications. Sometimes, a weighted graph is also called a network. Example: Data Structure 2016 R. Wei 47
48 Two methods can be used to store a weighted graph: Data Structure 2016 R. Wei 48
49 Minimum spanning tree A spanning tree contains all of the vertices in a connected graph. A minimum spanning tree is a spanning tree in which the total weight of the edges is the minimum of all possible spanning trees in the graph. Example: Find a minimum spanning tree for the network: Data Structure 2016 R. Wei 49
50 The main idea: Start from any vertex, say A. Put A into the set S. Consider all the edges connected to A and find out the minimum weight edge, say the edge connect to C. Put C in S and put edge AC to the tree T. In general, consider all the edges connected to a vertex in S, but the vertex of the other end of the edge is not in S. Select the minimum weight edge which is then put in the tree T. The vertex of the other end of the edge is also put in S. If S contains all the vertices of the graph, output T. Data Structure 2016 R. Wei 50
51 Data Structure 2016 R. Wei 51
52 Data structure for spanning tree of graph: We use adjacency list implementation which is similar to the previous implementation of graphs. The main differences are: For graph vertex, intree are used instead of processed. intree is a indicator to indicate if the vertex is included in the spanning tree. For graph edge, weight and intree are added. The weight used to record the weight of the edge and intree is used to indicate if the edge is included in the spanning tree. is Data Structure 2016 R. Wei 52
53 Data Structure 2016 R. Wei 53
54 Data Structure 2016 R. Wei 54
55 Shortest path: Dijkstra algorithm A shortest path between two vertices in a network is a path with minimum weights. Dijkstra algorithm is used to find the shortest path between any two nodes in a graph: Insert the first vertex into the tree. From every vertex already in the tree, check the total path to all adjacent vertices not in the tree. Select the edge with the minimum total path weight and insert it into the tree. Repeat above step until all vertices are in the tree. Data Structure 2016 R. Wei 55
56 Data Structure 2016 R. Wei 56
57 Data Structure 2016 R. Wei 57
58 Algorithm shortestpath (graph) Data Structure 2016 R. Wei 58
59 Data Structure 2016 R. Wei 59
60 The above algorithm is based on adjacency list. We also can use the adjacency matrix to find about the shortest path. Data Structure 2016 R. Wei 60
Chapter 9 STACK, QUEUE
Chapter 9 STACK, QUEUE 1 LIFO: Last In, First Out. Stacks Restricted form of list: Insert and remove only at front of list. Notation: Insert: PUSH Remove: POP The accessible element is called TOP. Stack
More informationCS 206 Introduction to Computer Science II
CS 206 Introduction to Computer Science II 04 / 06 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? Graphs Definition Terminology two ways to represent edges in implementation traversals
More informationCS 2412 Data Structures. Chapter 8 Multiway Trees
CS 2412 Data Structures Chapter 8 Multiway Trees This chapter studies multiway trees. These trees can be used for external search. When searched data is big, it is not suitable to load all the data to
More informationUnweighted Graphs & Algorithms
Unweighted Graphs & Algorithms Zachary Friggstad Programming Club Meeting References Chapter 4: Graph (Section 4.2) Chapter 22: Elementary Graph Algorithms Graphs Features: vertices/nodes/dots and edges/links/lines
More informationGraphs & Digraphs Tuesday, November 06, 2007
Graphs & Digraphs Tuesday, November 06, 2007 10:34 PM 16.1 Directed Graphs (digraphs) like a tree but w/ no root node & no guarantee of paths between nodes consists of: nodes/vertices - a set of elements
More informationROOT: A node which doesn't have a parent. In the above tree. The Root is A.
TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T 1, T 2...T k, each of whose roots are connected
More informationStacks and Queues. CSE Data Structures April 12, 2002
Stacks and Queues CSE 373 - Data Structures April 12, 2002 Readings and References Reading Section 3.3 and 3.4, Data Structures and Algorithm Analysis in C, Weiss Other References 12-Apr-02 CSE 373 - Data
More informationAbstract Data Types CHAPTER 12. Review Questions
PTR bstract ata Types Review Questions. n abstract data type is a data declaration packaged together with the operations that are meaningful for the data type with the implementation hidden from the user..
More informationOutline. Introduction. Representations of Graphs Graph Traversals. Applications. Definitions and Basic Terminologies
Graph Chapter 9 Outline Introduction Definitions and Basic Terminologies Representations of Graphs Graph Traversals Breadth first traversal Depth first traversal Applications Single source shortest path
More informationCMPT 125: Practice Midterm Answer Key
CMPT 125, Spring 2017, Surrey Practice Midterm Answer Key Page 1 of 6 CMPT 125: Practice Midterm Answer Key Linked Lists Suppose you have a singly-linked list whose nodes are defined like this: struct
More informationUNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.
UNIT III TREES A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items. Tree: A tree is a finite set of one or more nodes such that, there
More informationQueues. A queue is a special type of structure that can be used to maintain data in an organized way.
A queue is a special type of structure that can be used to maintain data in an organized way. This data structure is commonly implemented in one of two ways: as an array or as a linked list. In either
More informationList, Stack and Queue Implementation
Roy Chan CSC2100B Data Structures Tutorial 2 (Version 2) January 21, 2009 1 / 39 1 CSC2100B Online Judge Score 2 Structure 3 Linked List Overview Implementation 4 Stack Overview Implementation 5 Queue
More informationCSE 100: GRAPH ALGORITHMS
CSE 100: GRAPH ALGORITHMS 2 Graphs: Example A directed graph V5 V = { V = E = { E Path: 3 Graphs: Definitions A directed graph V5 V6 A graph G = (V,E) consists of a set of vertices V and a set of edges
More informationLECTURE 17 GRAPH TRAVERSALS
DATA STRUCTURES AND ALGORITHMS LECTURE 17 GRAPH TRAVERSALS IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD STRATEGIES Traversals of graphs are also called searches We can use either breadth-first
More informationUNIT IV -NON-LINEAR DATA STRUCTURES 4.1 Trees TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T1,
More informationCS302 - Data Structures using C++
CS302 - Data Structures using C++ Topic: Graphs - Introduction Kostas Alexis Terminology In the context of our course, graphs represent relations among data items G = {V,E} A graph is a set of vertices
More informationUNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list
UNIT-4 Graph: Terminology, Representation, Traversals Applications - spanning trees, shortest path and Transitive closure, Topological sort. Sets: Representation - Operations on sets Applications. 1. Name
More informationQueues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1
Queues October 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Queue ADT Queue ADT should support at least the first two operations: enqueue insert an item to the back of the queue dequeue remove an item from
More information12 Abstract Data Types
12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define
More informationLecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this
Lecture Notes Array Review An array in C++ is a contiguous block of memory. Since a char is 1 byte, then an array of 5 chars is 5 bytes. For example, if you execute the following C++ code you will allocate
More informationGraphs: Graph Data Structure:
Graphs: A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links. The interconnected objects are represented by points termed as vertices, and the links
More informationLists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4
Lists, Stacks and Queues in C CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4 Outline Structure Linked List Overview Implementation Stack Overview Implementation Queue Overview Implementation 2
More informationLecture 26: Graphs: Traversal (Part 1)
CS8 Integrated Introduction to Computer Science Fisler, Nelson Lecture 6: Graphs: Traversal (Part ) 0:00 AM, Apr, 08 Contents Introduction. Definitions........................................... Representations.......................................
More informationUNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:
UNIT 5 CSE 103 - Unit V- Graph GRAPH Graph is another important non-linear data structure. In tree Structure, there is a hierarchical relationship between, parent and children that is one-to-many relationship.
More informationCS6202 - PROGRAMMING & DATA STRUCTURES I Unit IV Part - A 1. Define Stack. A stack is an ordered list in which all insertions and deletions are made at one end, called the top. It is an abstract data type
More informationObjective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)
Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113) 1. The number of interchanges required to sort 5, 1, 6, 2 4 in ascending order using Bubble Sort (A)
More informationCS 206 Introduction to Computer Science II
CS 206 Introduction to Computer Science II 04 / 13 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? Graphs Depth First Search (DFS) Shortest path Shortest weight path (Dijkstra's
More informationFundamentals of Data Structure
Fundamentals of Data Structure Set-1 1. Which if the following is/are the levels of implementation of data structure A) Abstract level B) Application level C) Implementation level D) All of the above 2.
More informationA6-R3: DATA STRUCTURE THROUGH C LANGUAGE
A6-R3: DATA STRUCTURE THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be answered in the TEAR-OFF
More informationCS 2412 Data Structures. Chapter 4 Lists
CS 2412 Data Structures Chapter 4 Lists Definition A list is an ordered collection of data in which an addition or deletion can be performed at any position. In general a list is neither FIFO nor FILO.
More informationCSE 100: GRAPH SEARCH
CSE 100: GRAPH SEARCH Announcements PA3 released Checkpoint due Tuesday, May 5 @10:00pm Final submission due Thursday, May 14 @10:00PM Start early! Start early! Start early! Start early! Start early! I
More informationStack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5
What is Stack? Stack 1. Stack is LIFO Structure [ Last in First Out ] 2. Stack is Ordered List of Elements of Same Type. 3. Stack is Linear List 4. In Stack all Operations such as Insertion and Deletion
More informationGraphs ADT and Traversing
Steven J. Zeil July 31, 2013 Contents 1 A Graph ADT 3 1.1 Vertex...................................... 4 1.2 Edge....................................... 7 1.3 DiGraph....................................
More information.:: UNIT 4 ::. STACK AND QUEUE
.:: UNIT 4 ::. STACK AND QUEUE 4.1 A stack is a data structure that supports: Push(x) Insert x to the top element in stack Pop Remove the top item from stack A stack is collection of data item arrange
More informationData Structure Advanced
Data Structure Advanced 1. Is it possible to find a loop in a Linked list? a. Possilbe at O(n) b. Not possible c. Possible at O(n^2) only d. Depends on the position of loop Solution: a. Possible at O(n)
More informationSELF-BALANCING SEARCH TREES. Chapter 11
SELF-BALANCING SEARCH TREES Chapter 11 Tree Balance and Rotation Section 11.1 Algorithm for Rotation BTNode root = left right = data = 10 BTNode = left right = data = 20 BTNode NULL = left right = NULL
More informationCSC Intro to Intelligent Robotics, Spring Graphs
CSC 445 - Intro to Intelligent Robotics, Spring 2018 Graphs Graphs Definition: A graph G = (V, E) consists of a nonempty set V of vertices (or nodes) and a set E of edges. Each edge has either one or two
More informationChapter 9. Priority Queue
Chapter 9 Priority Queues, Heaps, Graphs Spring 2015 1 Priority Queue Priority Queue An ADT in which only the item with the highest priority can be accessed 2Spring 2015 Priority Depends on the Application
More informationEnd-Term Examination Second Semester [MCA] MAY-JUNE 2006
(Please write your Roll No. immediately) Roll No. Paper Code: MCA-102 End-Term Examination Second Semester [MCA] MAY-JUNE 2006 Subject: Data Structure Time: 3 Hours Maximum Marks: 60 Note: Question 1.
More informationFundamental Algorithms
Fundamental Algorithms Chapter 8: Graphs Jan Křetínský Winter 2017/18 Chapter 8: Graphs, Winter 2017/18 1 Graphs Definition (Graph) A graph G = (V, E) consists of a set V of vertices (nodes) and a set
More informationlecture27: Graph Traversals
lecture27: Largely based on slides by Cinda Heeren CS 225 UIUC 25th July, 2013 Announcements mp7.1 extra credit due tomorrow night (7/26) Code challenge tomorrow night (7/26) at 6pm in 0224 lab hash due
More informationCpt S 122 Data Structures. Course Review Midterm Exam # 1
Cpt S 122 Data Structures Course Review Midterm Exam # 1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 1 When: Friday (09/28) 12:10-1pm Where:
More informationR13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)
SET - 1 II B. Tech I Semester Supplementary Examinations, May/June - 2016 PART A 1. a) Write a procedure for the Tower of Hanoi problem? b) What you mean by enqueue and dequeue operations in a queue? c)
More informationS.E. Sem. III [INFT] Data Structures & Analysis. Primitive Linear Non Linear
S.E. Sem. III [INFT] Data Structures & Analysis Time : 3 Hrs.] Prelim Paper Solution [Marks : 80 Q.1(a) Explain different types of data structures with examples. [5] Ans.: Types of Data Structure : Data
More informationChapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1
Chapter 14 Graphs 2011 Pearson Addison-Wesley. All rights reserved 14 A-1 Terminology G = {V, E} A graph G consists of two sets A set V of vertices, or nodes A set E of edges A subgraph Consists of a subset
More informationGRAPHS Lecture 17 CS2110 Spring 2014
GRAPHS Lecture 17 CS2110 Spring 2014 These are not Graphs 2...not the kind we mean, anyway These are Graphs 3 K 5 K 3,3 = Applications of Graphs 4 Communication networks The internet is a huge graph Routing
More information1. Graph and Representation
Chapter Graphs Tree Root Direction: parent-child relationships No cycles Graph Vertices + Edges No tree restrictions Examples World Wide Web Maps. Graph and Representation A graph G: a pair (V, E) vertices
More informationGraphs. Outline. Definitions Graph implementation as data structure Visiting algorithms C implementations
Graphs Outline Definitions Graph implementation as data structure Visiting algorithms C implementations Directed Graph Directed graph G (also called digraph) is a pair (V,E), where V is the (finite) set
More informationGraph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph
Graphs Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs start Birmingham 60 Rugby fill pan with water add salt to water take egg from fridge
More informationBasic Graph Definitions
CMSC 341 Graphs Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge is a pair (v,w) where v, w V. V and E are sets, so each vertex
More information- Logic and Algorithms - Graph Algorithms
Fundamentals of Computer Sience and Digital Communications - Logic and Algorithms - Graph Algorithms Johan Larsson Marco Loh 22..24 Overview What is a Graph? Representations of Graphs Breadth-first Search
More informationCS 241 Data Organization Binary Trees
CS 241 Data Organization Binary Trees Brooke Chenoweth University of New Mexico Fall 2017 Binary Tree: Kernighan and Ritchie 6.5 Read a file and count the occurrences of each word. now is the time for
More informationGraph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1
Graph Algorithms Chapter 22 CPTR 430 Algorithms Graph Algorithms Why Study Graph Algorithms? Mathematical graphs seem to be relatively specialized and abstract Why spend so much time and effort on algorithms
More informationINSTITUTE OF AERONAUTICAL ENGINEERING
INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 500 043 COMPUTER SCIENCE AND ENGINEERING TUTORIAL QUESTION BANK Course Name Course Code Class Branch DATA STRUCTURES ACS002 B. Tech
More informationDirected Graph and Binary Trees
and Dr. Nahid Sultana December 19, 2012 and Degrees Paths and Directed graphs are graphs in which the edges are one-way. This type of graphs are frequently more useful in various dynamic systems such as
More informationTrees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology
Trees : Part Section. () (2) Preorder, Postorder and Levelorder Traversals Definition: A tree is a connected graph with no cycles Consequences: Between any two vertices, there is exactly one unique path
More informationChapter 9 Graph Algorithms
Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 3 Definitions an undirected graph G = (V, E)
More informationComputer Science Foundation Exam
Computer Science Foundation Exam January 12, 2019 Section I A DATA STRUCTURES NO books, notes, or calculators may be used, and you must work entirely on your own. Name: UCFID: NID: Question # Max Pts Category
More informationGraphs. The ultimate data structure. graphs 1
Graphs The ultimate data structure graphs 1 Definition of graph Non-linear data structure consisting of nodes & links between them (like trees in this sense) Unlike trees, graph nodes may be completely
More informationCpt S 122 Data Structures. Data Structures
Cpt S 122 Data Structures Data Structures Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Self Referential Structures Dynamic Memory Allocation
More informationGraph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship
Graph Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance or cost) path Graph Theory Many problems are mapped
More informationCS 310 Advanced Data Structures and Algorithms
CS 31 Advanced Data Structures and Algorithms Graphs July 18, 17 Tong Wang UMass Boston CS 31 July 18, 17 1 / 4 Graph Definitions Graph a mathematical construction that describes objects and relations
More information11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions
Introduction Chapter 9 Graph Algorithms graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 2 Definitions an undirected graph G = (V, E) is
More informationThe ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value
The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value 1 Possible implementations Sorted linear implementations o Appropriate if
More informationThis course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.
Lecture 9 Graphs This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. You need to be familiar with the design and use of basic data structures such as Lists, Stacks,
More informationCPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues
CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues Alan J. Hu (Slides borrowed from Steve Wolfman) Be sure to check course webpage! http://www.ugrad.cs.ubc.ca/~cs221 1 Lab 1 is available.
More informationCP2 Revision. theme: dynamic datatypes & data structures
CP2 Revision theme: dynamic datatypes & data structures structs can hold any combination of datatypes handled as single entity struct { }; ;
More informationIntroduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis
Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis p. 5 Statement Constructs p. 5 Pseudocode Example p.
More informationGRAPHS Lecture 19 CS2110 Spring 2013
GRAPHS Lecture 19 CS2110 Spring 2013 Announcements 2 Prelim 2: Two and a half weeks from now Tuesday, April16, 7:30-9pm, Statler Exam conflicts? We need to hear about them and can arrange a makeup It would
More informationLinked List. April 2, 2007 Programming and Data Structure 1
Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element
More informationBreadth First search and Depth First search Finding connected components
Breadth First search and Depth First search Finding connected components Biswadeep Ghosh March 15, 2018 1 Contents 1 Binary Image 4 2 Graphs 4 2.1 Definition.................................... 4 2.2 Example......................................
More informationWhere does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).
Suppose we have a circular array implementation of the queue,with ten items in the queue stored at data[2] through data[11]. The current capacity of an array is 12. Where does the insert method place the
More informationData Structures and Algorithms for Engineers
4-63 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmuedu wwwvernoneu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University Africa
More informationUnderstand graph terminology Implement graphs using
raphs Understand graph terminology Implement graphs using djacency lists and djacency matrices Perform graph searches Depth first search Breadth first search Perform shortest-path algorithms Disjkstra
More information15. Stacks and Queues
COMP1917 15s2 15. Stacks and Queues 1 COMP1917: Computing 1 15. Stacks and Queues Reading: Moffat, Section 10.1-10.2 Overview Stacks Queues Adding to the Tail of a List Efficiency Issues Queue Structure
More informationComputer Science E-22 Practice Final Exam
name Computer Science E-22 This exam consists of three parts. Part I has 10 multiple-choice questions that you must complete. Part II consists of 4 multi-part problems, of which you must complete 3, and
More informationQueue Linked List Implementation
SCJ2013 Data Structure & Algorithms Queue Linked List Implementation Nor Bahiah Hj Ahmad Queue Implementation Link List Pointer Based Implementation Can be implemented using linear linked list or circular
More informationLinear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013
Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every
More informationSample Question Paper
Scheme - I Sample Question Paper Marks : 70 Time: 3 Hrs. Q.1) Attempt any FIVE of the following. 10 Marks a. Write any four applications of data structure. b. Sketch the diagram of circular queue. c. State
More informationGraphs. The ultimate data structure. graphs 1
Graphs The ultimate data structure graphs 1 Definition of graph Non-linear data structure consisting of nodes & links between them (like trees in this sense) Unlike trees, graph nodes may be completely
More informationGraph Traversals BFS & DFS 1 CS S-16
CS-8S- BFS & DFS -: Visit every vertex, in an order defined by the topololgy of the graph. Two major traversals: Depth First Search Breadth First Search -: Depth First Search Starting from a specific node
More informationCS 62 Practice Final SOLUTIONS
CS 62 Practice Final SOLUTIONS 2017-5-2 Please put your name on the back of the last page of the test. Note: This practice test may be a bit shorter than the actual exam. Part 1: Short Answer [32 points]
More informationCS 2412 Data Structures. Chapter 7 Heaps
CS 2412 Data Structures Chapter 7 Heaps A binary tree is a complete tree if it has maximum number of entries for its height. a nearly complete if it has the minimum height for its nodes and all nodes in
More informationcsci 210: Data Structures Graph Traversals
csci 210: Data Structures Graph Traversals Graph traversal (BFS and DFS) G can be undirected or directed We think about coloring each vertex WHITE before we start GRAY after we visit a vertex but before
More informationIntroduction to Graphs. common/notes/ppt/
Introduction to Graphs http://people.cs.clemson.edu/~pargas/courses/cs212/ common/notes/ppt/ Introduction Graphs are a generalization of trees Nodes or verticies Edges or arcs Two kinds of graphs irected
More informationDIRECTED GRAPHS BBM 201 DATA STRUCTURES DEPT. OF COMPUTER ENGINEERING
Acknowledgement: he course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University. BBM DAA SRUCURES DEP. O COMPUER ENGINEERING DIRECED GRAPHS Directed Graphs Digraph
More information18: GRAPH DATA STRUCTURES. Introduction
18: GRAPH DATA STRUCTURES Introduction... 1 Describing graphs... Directed Graphs... 3 Traversing a graph... EXERCISE: Traversal... 8 Implementing a Graph... 9 EXERCISE: Looking at a Graph... 1 EXERICISE:
More informationAlgorithm Design (8) Graph Algorithms 1/2
Graph Algorithm Design (8) Graph Algorithms / Graph:, : A finite set of vertices (or nodes) : A finite set of edges (or arcs or branches) each of which connect two vertices Takashi Chikayama School of
More informationROOT A node which doesn t have a parent. In the above tree. The Root is A. LEAF A node which doesn t have children is called leaf or Terminal node.
UNIT III : DYNAMIC STORAGE MANAGEMENT Trees: Binary tree, Terminology, Representation, Traversals, Applications. Graph: Terminology, Representation, Traversals Applications - spanning trees, shortest path.
More informationLecture 9 Graph Traversal
Lecture 9 Graph Traversal Euiseong Seo (euiseong@skku.edu) SWE00: Principles in Programming Spring 0 Euiseong Seo (euiseong@skku.edu) Need for Graphs One of unifying themes of computer science Closely
More informationLinear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013
Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every
More informationData Structures and Algorithms Key to Homework Assignment 8
Data Structures and Algorithms Key to Homework Assignment 8. Apply the strong components algorithm to the digraph that has the following adjacency matrix: 0 4 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
More informationCSI 604 Elementary Graph Algorithms
CSI 604 Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. (Second edition) 1 / 25 Graphs: Basic Definitions Undirected Graph G(V, E): V is set of nodes (or vertices) and E is the
More informationChapter 16: Graphs and Digraphs
Chapter 16: Graphs and Digraphs Exercises 16.1 1. adj: 2. adj: 3. adj: 4. adj: 5. E 1 1 0 1 1 1 1 0 0 1 1 1 1 0 1 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 1
More informationGraph. Vertex. edge. Directed Graph. Undirected Graph
Module : Graphs Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS E-mail: natarajan.meghanathan@jsums.edu Graph Graph is a data structure that is a collection
More informationAdvanced Data Structures and Algorithms
dvanced ata Structures and lgorithms ssociate Professor r. Raed Ibraheem amed University of uman evelopment, College of Science and Technology Computer Science epartment 2015 2016 epartment of Computer
More informationC Data Structures Stacks. Stack. push Adds a new node to the top of the stack
1 12 C Data Structures 12.5 Stacks 2 Stack New nodes can be added and removed only at the top Similar to a pile of dishes Last-in, first-out (LIFO) Bottom of stack indicated by a link member to NULL Constrained
More informationGraphs and Genetics. Outline. Computational Biology IST. Ana Teresa Freitas 2015/2016. Slides source: AED (MEEC/IST); Jones and Pevzner (book)
raphs and enetics Computational Biology IST Ana Teresa Freitas / Slides source: AED (MEEC/IST); Jones and Pevzner (book) Outline l Motivacion l Introduction to raph Theory l Eulerian & Hamiltonian Cycle
More informationCS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.
CS 1114: Implementing Search! Prof. Graeme Bailey http://cs1114.cs.cornell.edu (notes modified from Noah Snavely, Spring 2009) Last time! Graph traversal 1 1 2 10 9 2 3 6 3 5 6 8 5 4 7 9 4 7 8 10! Two
More information