Data Types and Data Structures When specifying data

  • Slides: 22
Download presentation
Data Types and Data Structures • When specifying data in a program we need

Data Types and Data Structures • When specifying data in a program we need to describe its type and its structure. • Data's type impose meaning onto data (semantics) and data's structure impose organization (syntax)onto data. • Data Type (definition): A label applied to data that tells the computer how to interpret and manipulate data. – Type tells the computer how much space to reserve for variables and how to interpret operations on them. • Data Structure (definition): The way data is organized logically. – Describes how different pieces of data are organized.

Data Structures - Atomic Name Atomic Definition A data structure containing a single value,

Data Structures - Atomic Name Atomic Definition A data structure containing a single value, or data item, of any type. Std Functions Assign – places a value in the atomic structure. Retrieve – returns the value currently stored in the structure. Non-Std Functions Notes None This is the most basic data structure, from which all others are built. Contains only one data item Can be placed anywhere in memory.

Data Structures - Array Name Array Definition A group of data elements stored in

Data Structures - Array Name Array Definition A group of data elements stored in contiguous memory. Std Functions Assign(n) – store a value in the nth element. Retrieve(n) – retrieves the value stored in the nth element. Non-Std Functions Notes Length – returns number of elements in the array. Sort – sorts the elements of the array. Matrix operations, like determinant, transpose, etc. Typically, all elements of an array are of the same type. Typically, each element in an array can be of any type. Advantage: Instant access to any element in the array (ie it is just as easy to retrieve the value of the first element as any other). Disadvantages: Hard to change the size of the array. Hard to insert new elements in the middle of the array.

Data Structures - Linked List Name Linked List Definition A group of data elements

Data Structures - Linked List Name Linked List Definition A group of data elements composed of two parts: a value part and a link part. The link points to the next data element in the list. Std Functions Insert(value) – inserts a new element into the list. Remove(value) – removes an element from the list. Non-Std Functions Notes None The variable representing the linked list points to the head of the list. The last element in the list has a value of “NULL” for its link. Advantages: The linked list can change size easily. Elements can be inserted and deleted easily into linked lists. Disadvantage: You do not have quick access to members 1 2 3 4

Data Structures - Stack Name Stack Definition A structure in which only the top

Data Structures - Stack Name Stack Definition A structure in which only the top element is visible. Std Functions Push(value) – pushes the value on the top of the stack. Pop() – Removes the top value in a stack. Peek() – Returns the value of the top element on the stack. Non-Std Functions Push(value 1, value 2, value 3. . ) - pushes multiple values on the stack. Pop(n) – Pops n elements off the stack. Peek(n) - Returns the value of the nth element. Can be though of as a stack of plates or trays. Notes Exhibits LIFO (Last in First Out) behavior. Useful in task scheduling in which one task must be completed before others can begin.

Data Structures - Queue Name Queue Definition A list of elements in which elements

Data Structures - Queue Name Queue Definition A list of elements in which elements are added to one end and removed from the other. Std Functions Enqueue(value) – adds the value to one end of the queue. Dequeue() – removes a value from the queue. Non-Std Functions Notes None Can be thought of as a line of customers, such as at the bank or grocers. Exhibits FIFO (First in First Out) behavior. Useful in task scheduling in which FIFO is important (Example: printer queues)

Data Structures - Stacks & Queues ENTER EXIT STACK ENTER EXIT QUEUE NOTE: STACKS

Data Structures - Stacks & Queues ENTER EXIT STACK ENTER EXIT QUEUE NOTE: STACKS AND QUEUES ARE TYPICALLY DEPICTED LIKE ARRAYS BUT THE INDIVIDUAL ELEMENTS CAN RESIDE ANYWHERE IN MEMORY.

Data Structures - Graph Name Graph Definition A set of nodes (data elements) connected

Data Structures - Graph Name Graph Definition A set of nodes (data elements) connected by edges in an arbitrary manner. Std Functions None Non-Std Functions Notes None The most versatile data structure (linked lists, trees and heaps are special instances of graphs). Standard Problems: Graph Coloring: Coloring the nodes of a graph such that adjacent nodes do not have the same color. Traveling Salesman: Visiting each node in the graph exactly once for the least cost (start and stop at the same node). Maximum Flow: Determine the amount of flow through a network.

Data Structures - Tree Name Tree Definition A graph with directed edges connecting parent

Data Structures - Tree Name Tree Definition A graph with directed edges connecting parent to child such that there is exactly one node with no parents and all other nodes have exactly one parent. Std Functions None Non-Std Functions Notes None The first element in the tree is the “root” node, which has no parents, and from which all others can be reached. Nodes with no children are "leaf" nodes. If nodes “a” and “b” are connected by an edge, then: “a” is a child of “b” is closer to the root than “a” is a parent of “b” if “a” is closer to the root than “b” Useful in making decisions and categorizing data.

Data Structures - Heap Name Heap Definition A tree in which a parent node

Data Structures - Heap Name Heap Definition A tree in which a parent node has a value larger than all its children. Std Functions Heap(a, H) – Add new node a to heap H. Unheap(H) – Remove the root element from heap H and reestablish the heap. Non-Std Functions Notes None Flexible data structure useful for sorting elements “as they arrive. ” This allows sorting on lists whoses size change constantly. Used in “priority queues” or other situations where maintaining and accessing a maximum element is important.

Data Structures - Graph THESE ARE ALL GRAPHS.

Data Structures - Graph THESE ARE ALL GRAPHS.

Data Structures - Graph THESE ARE TREES (ABOVE). ARE THEY HEAPS? THESE ARE NOT

Data Structures - Graph THESE ARE TREES (ABOVE). ARE THEY HEAPS? THESE ARE NOT TREES (ABOVE). ARE THEY HEAPS?

Graph Problems • There are literally thousands of graph problems, but we will focus

Graph Problems • There are literally thousands of graph problems, but we will focus on three that are occur very commonly and show the diversity of the graph structure: – The Traveling Salesman Problem. – Graph Coloring Problem. – Maximum Flow Problem. • Each problem has a decision form and an optimization form. The decision form asks "Can we do it? " and the optimization form asks "How well can we do it? " • At least one of these problems is solved by you every day without you realizing it (until now). • The fact that the nodes and edges can represent anything means that the graph structure is very versatile and virtually any problem can be mapped to a graph problem.

Graph Problems - Traveling Salesman • Description: Given a graph, G = {N, E},

Graph Problems - Traveling Salesman • Description: Given a graph, G = {N, E}, where – N = a set of cities. – E = travel routes between the cities, each having a cost associated with it. – One special node, s. – You must begin at city sand travel to each of the other cities exactly once and then return to city s. Thus you make a complete cycle of all cities in the graph. • Decision form of the problem: Can a route be found where the total cost of the trip is less than X? (Answer is yes or no). • Optimization form of the problem: What is the absolute lowest cost?

Graph Problems - Graph Coloring • Description: Given a graph, G = {N, E},

Graph Problems - Graph Coloring • Description: Given a graph, G = {N, E}, where – N = a set of nodes. – E = edges between the nodes. – The object is to color the graph such that no nodes connecte by an edge have the same color. • Decision form of the problem: Can the graph be colored with X or less colors? (Answer is yes or no). • Optimization form of the problem: What is the fewest number of colors required to color the graph?

Graph Problems - Maximum Flow • Description: Given a graph, G = {N, E},

Graph Problems - Maximum Flow • Description: Given a graph, G = {N, E}, where – N = a set of nodes. – E = edges representing pipes, each assigned a given capacity. – Two special nodes. Node s is a source node that can potentially spit out an infinite amount of material. Node f is a sink node that can potentially absorb an infinite amount of material. – The object is to determine the maximum amount of material that can flow through the network for the source to the sink. • Decision form of the problem: Can X amount of material be pushed through the network from the source to the sink? (Answer is yes or no). • Optimization form of the problem: What is the maximum amount of material that can flow through the material from the source to the sink?

Cost of Graph Problems Name Description Cost * Comments Traveling Salesman Decision Does a

Cost of Graph Problems Name Description Cost * Comments Traveling Salesman Decision Does a route exist with cost less than X? O(n!) Hard to solve, easy to verify “yes” answers. Traveling Salesman Optimization What is the least cost route. O(n!) Hard to solve, hard to verify. Graph Coloring Decision Can a graph be colored properly using X colors? O(kn) where k is the number of colors Hard to solve, easy to verify “yes” answers. Graph Coloring Optimization What is the least number of colrs required to properly color a graph? O(kn) where k is the number of colors. Hard to solve, hard to verify. Maximum Flow What is the most material that can be pushed O(n 3) Polynomial solution time means easy to solve, easy to verify.

Cost of Typical Problems Name Description Cost * Get. Max What is the largest

Cost of Typical Problems Name Description Cost * Get. Max What is the largest number in an unordered list of numbers? O(n) Search (Unordered list) Given a list and a key, determine if the key is in the list. O(n) Search (Ordered List) Given a list and a key, determine if the key is in the list. O(log n) Sort (Bubble. Sort) Given an unordered list, rank all elements from smallest to largest. O(n 2) Sort (Quick. Sort, Merge. Sort, Heap. Sort) Given an unordered list, rank all elements from smallest to largest. O(n * log n) Sort (Problem) Given an unordered list, rank all elements from smallest to largest. O(n * log n)

Artificial Intelligence • Artificial Intelligence (AI) is the name given to encoding intelligent or

Artificial Intelligence • Artificial Intelligence (AI) is the name given to encoding intelligent or humanistic behaviors in computer software. • Problem: Nobody has created a widely accepted definition of intelligence. – At one time was considered a uniquely human quality. – Now generally accepted to be an animal quality. – Has been linked to tool use, tool creation, learning, adaptation to novel situations, capacity for abstraction. • Problem: Nobody has created a widely accepted definition of artificial intelligence. – Cognitive models attempt to recreate the actual processes of the human brain. – Behavioral models attempt to produce behavior that is reasonable for a situation regardless of how the behavior was produced. – Tend to focus on reasoning, behavior, learning, adaptation.

Artificial Intelligence Challenges • Format and Size of Knowledge – the data structures we

Artificial Intelligence Challenges • Format and Size of Knowledge – the data structures we have discussed so far capture data values, but not data meaning. How is knowledge represented? How are relationships between • Ambiguity – Knowledge ultimately represents natural phenomena that are inherently ambiguous. How do we resolve this?

Proposed AI Systems • Rule Based Behavior – designed behavior specifying sets of conditions

Proposed AI Systems • Rule Based Behavior – designed behavior specifying sets of conditions and responses. – Wealth and complexity of rules limits applications. • Case-based and Context-Based Reasoning – attempt to reduce search space of possible behaviors by only considering those associated with certain situations or contexts.

Proposed AI Systems • Emergent Behavior (Ant Logic) – Overall behavior resulting from the

Proposed AI Systems • Emergent Behavior (Ant Logic) – Overall behavior resulting from the interaction of smaller rule sets or weak individual agents. Overall behavior is not designed but desired. • Genetic Algorithms – represents behavioral rules as long strings, termed “genomes. ” Behavior is evolved as various genomes are tried and evaluated. Higher rated genomes are allowed to survive and “reproduce” with other high ranking genomes. • Synthetic Social Structures – Models more complex animal social behaviors, such as those found in herds and packs. Allows efficient interaction without much communication.